0

嗨我有以下代码,当我第一次调用 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";
    }

一旦第二次调用该方法被杀死,我该如何解决这个问题,

感谢您的时间,这个应用程序是在 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 
4

1 回答 1

0

您的问题似乎是由于table多次尝试为变量设置相同的值而引起的。您可以通过执行以下操作来修复它:

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;
}

这不是一个精确的解决方案,也没有以任何方式进行测试,但希望能让您了解尝试什么。关键是仅table在尚未完成的情况下创建和设置值(简单的单例模式)。

PS:如果您想/需要,您还可以检查表是否包含"gamekey"and "countdown",而不是像示例中那样仅检查是否是null

于 2012-09-06T07:22:12.530 回答