簡體   English   中英

活動中途停止

[英]The event stops halfway

我正在執行一個由事件調用的異步匿名方法,但它在中途終止而不執行一半代碼。

方法,當事件調用時調用。

private void OnBlockGenerationTime()
        {
            Task.Run(async () =>
            {
                await Task.Delay(10);

                if (!ChainMutex.WaitOne(3000))
                    throw new TimeoutException("Cannot access to blockchain, mutex was held too long.");

                var winnerTrx = PoS.GetWinner(Blockchain.LatestBlock.ID);

                ChainMutex.ReleaseMutex();

                if (winnerTrx == null)
                    return;

                var winner = PoolsSet.TransactionPool.GetTxOut(winnerTrx.Trx.Inputs[0]).Address;

                if (winner == CurrentAccount.GetAddress())
                {
                    var (block, reward) = Blockchain.CreateBlock(CurrentAccount.GetPrivateKey()); //here task terminating.

                    CommonLog.Log(LogElementType.OK, $"Our stake won. A new block with ID {block.ID} was created. {reward} AWT were collected.");

                    P2P.BroadcastLatestBlock();
                }
            });
        }

具有無限任務的方法,調用事件:

private void RunNewBlockListening()
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    while (GetRemainingToNewBlock() != 0) { await Task.Delay(100); }

                    OnNewBlockGeneration?.Invoke();

                    await Task.Delay(1000);
                }
            });
        }

我知道異步方法以不同的方式執行,但是標記的代碼在 debagged 時永遠不會執行。

感謝大家的幫助,那些正在尋找在匿名函數中捕獲異常的答案的人,只需單擊“異常參數”選項卡(調試 - > Windows - > 異常參數)中的“公共語言運行時異常”復選框。 就這樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM