0
HI我有以下代碼,當我第一次調用Ping方法時,它可以工作,但第二次調用失敗,並且已存在錯誤 Ping服務意味着永葆16秒,一旦定時器計數爲零的用戶從數據表中刪除,這樣我有當前連接的用戶使用datatable在web服務中使用datatable進行倒計時的獨特倒數計時器
列表 public class PokerHost : WebService
{
//bool RunningTimmer = true;
static DataTable table = new DataTable();
private static readonly TimeSpan UpdateEngineTimerFrequency = TimeSpan.FromSeconds(2);
private Timer UpdateEngineTimer { get; set; }
private void MyTimerAction(object state)
{
DataTable table = GetTable(); // Get the data table.
foreach (DataRow row in table.Rows) // Loop over the rows.
{
int minused = Convert.ToInt32(row["countdown"]) - 2;
if (minused >= 0) {
row["countdown"] = minused;
}
else
{
row.Delete();
}
table.AcceptChanges();
}
}
static DataTable GetTable()
{
table.Columns.Add("gamekey", typeof(string));
table.Columns.Add("countdown", typeof(int));
return table;
}
protected void Application_Start(object sender, EventArgs e)
{
this.UpdateEngineTimer = new Timer(MyTimerAction,
null, /* or whatever state object you need to pass */
UpdateEngineTimerFrequency,
UpdateEngineTimerFrequency);
}
protected void Application_End(object sender, EventArgs e)
{
this.UpdateEngineTimer.Dispose();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
//--Only webmethods
//--------------------------------------------------------------------------------------------------------------------------------------------------
[WebMethod]
public string Ping(string gamekey)
{
DataTable table = GetTable(); // Get the data table.
foreach (DataRow row in table.Rows) // Loop over the rows.
{
if (Convert.ToString(row["gamekey"]) == gamekey)
{
row["countdown"] = 16;
}
table.AcceptChanges();
table.Dispose();
}
//make array of current online users
// need to check with the game and timelimits
table.AcceptChanges();
table.Dispose();
return "PONG";
}
只要方法被稱爲第二次的被殺,我該如何解決這個問題,
感謝您的時間,這個應用程序是在單聲道,在Ubuntu上運行服務器
我得到的錯誤是這樣
500 - Internal Server Error
System.Data.DuplicateNameException: A DataColumn named 'gamekey' already belongs to this DataTable.
at System.Data.DataColumnCollection.RegisterName (System.String name, System.Data.DataColumn column) [0x00000] in <filename unknown>:0
at System.Data.DataColumnCollection.Add (System.Data.DataColumn column) [0x00000] in <filename unknown>:0
你最大的謝意,代碼現在工作的很好,創建的關鍵不需要檢查爲null,因爲如果它的null程序重新創建時它檢查,但否則一切正常,對不起,我不能標記你的答案我沒有分;)謝謝你的時間 – user1264626
謝謝,我的榮幸!很高興有幫助! :) – Kjartan