Home > Mobile >  The event stops halfway
The event stops halfway

Time:06-25

I am executing an asynchronous anonymous method that is called by an event, but it terminates halfway through without executing half the code.

Method, that called when an event called.

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

Method with infinte task, that calles event:

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

                    OnNewBlockGeneration?.Invoke();

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

I know that asynchronous methods are executed in different ways, but the marked code is never executed when debagged.

CodePudding user response:

Thank you all for your help, those who are looking for an answer to catching exceptions in anonymous functions, just click on the "Common Language Runtime Exceptions" checkbox in the "Exception parameters" tab (Debugging -> Windows -> Exception Parameters). That's all.

  • Related