Task.Delay 使用 SignalR 组创建 System.ObjectDisposedException:'Cannot access a disposed object'

Task.Delay creating System.ObjectDisposedException: 'Cannot access a disposed object' with SignalR group

我正在用 asp.net 内核和 Signalr 编写代码,其中有一个方法:

public async void newGameTimer(int tableID )
        {
            bool stop = true;
            int val = 0;
            while (stop)
            {
                await Task.Delay(1000);
                val++;
                if (val > 5)
                {
                    stop = false;
                    newGame(tableID);
                }
            }
        }

然后在newGame(tableID)方法中,如下:

public async void newGame(int tableID)
{
    await Clients.Group(group).SendAsync("updateTotalPot", 200);
}

我正在通过 signalR 组向玩家组发送异步响应。但它给出了错误

'System.ObjectDisposedException: '无法访问已释放的对象。 ObjectDisposed_ObjectName_Name'

我进行了搜索,得到的回答是 'Task.Dealy()' can create ObjectDisposed 错误。 现在看不懂,请问如何解决。

我只想等一段时间再回复给一群玩家..有没有人可以帮我?

项目:在线实时扑克游戏 扑克在线游戏,一群朋友可以创建一个 table 并一起玩扑克。 前端:反应 后端:asn.net 核心、Signalr、Redis

逻辑: 第一个玩家通过调用方法创建 table:

public async Task LoginWindow(string name, string password, int newTable, int joinTable)
{ 
   //- this method saves many keys in Redis in-memory like this
    dbr.StringSet("table" + "ToBet", 0);

  //- at end send reply with following command
     await Clients.Caller.SendAsync("updateMessage",  "Welcome to table no " + tableID);

//- then this method check, if second player has called this method, then calls the timer to start game after 5 seconds

  newGameTimer(tableID);
 
}


nothing else...

这里不需要async void。解决方案可能如下所示。

public async Task NewGameWithDelay(int tableID)
{
    for (int i = 0; i < 5; i++)
    {
        if (await CheckForSecondPlayer())
        {
            // found
            await Clients.Group(group).SendAsync("updateTotalPot", 200);
            return;
        }
        await Task.Delay(1000);
    }
    // not found
}

private async Task<bool> CheckForSecondPlayer()
{
    // if found return true, else
    return false;
}
public async Task LoginWindow(string name, string password, int newTable, int joinTable)
{ 
    dbr.StringSet("table" + "ToBet", 0);
    await Clients.Caller.SendAsync("updateMessage",  "Welcome to table no " + tableID);
    await NewGameWithDelay(tableID);
}