独特的倒计时器使用数据表在Web服务中使用倒计时

时间:2012-09-06 06:24:20

标签: c# .net web-services mono

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

1 个答案:

答案 0 :(得分: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""countdown",而不仅仅是检查它是否为null,如示例所示。

相关问题