C# 在web服务中使用datatable进行倒计时的唯一倒计时计时器

C# 在web服务中使用datatable进行倒计时的唯一倒计时计时器,c#,.net,web-services,mono,C#,.net,Web Services,Mono,嗨,我有下面的代码,当我第一次调用Ping方法时,它可以工作,但是在第二次调用时它失败了,错误已经存在 Ping服务意味着保持活动状态16秒,一旦计时器计数为零,用户就会从数据表中删除,这样我就有了一个当前连接用户的列表 public class PokerHost : WebService { //bool RunningTimmer = true; static DataTable table = new DataTable(); private

嗨,我有下面的代码,当我第一次调用Ping方法时,它可以工作,但是在第二次调用时它失败了,错误已经存在 Ping服务意味着保持活动状态16秒,一旦计时器计数为零,用户就会从数据表中删除,这样我就有了一个当前连接用户的列表

        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";
    }
一旦该方法第二次被调用,我该如何修复它

感谢您的时间,这个应用程序是用mono制作并运行在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 
500-内部服务器错误
System.Data.DuplicateNameException:名为“gamekey”的数据列已属于此数据表。
在:0中的System.Data.DataColumnCollection.RegisterName(System.String名称,System.Data.DataColumn列)[0x00000]处
在0中的System.Data.DataColumnCollection.Add(System.Data.DataColumn列)[0x00000]处

您的问题似乎是由于多次尝试将相同的值设置到变量
表中造成的。您可以通过执行以下操作来修复它:

static DataTable table = null;

static DataTable GetTable()
{
    if(table == null){
        table = new DataTable();
        table.Columns.Add("gamekey", typeof(string));
        table.Columns.Add("countdown", typeof(int));
    }
    return table;
}
这不是一个精确的解决方案,也没有以任何方式进行测试,但希望能让您知道应该尝试什么。要点是,如果尚未创建并设置值到
(一个简单的单例模式),则只创建并设置值到


PS:如果您想/需要,还可以检查表是否包含
“gamekey”
“countdown”
,而不是像示例中那样只检查它是否为
null

非常感谢,代码现在运行得很好,创建的密钥不需要检查null,因为如果它为null,程序将在其签入时重新创建它,但其他一切正常,抱歉我无法标记您的答案我没有分数;)谢谢你的时间谢谢,我的荣幸!很高兴能帮上忙!:)