c# - async await execution order - code only actually works when stepping through/debugging -


I hope there is a simple answer here, and this is probably due to my misunderstanding of asynchronous tasks ...

I have a way that can be started manually or programmatically can start automatically after loading. However, when the method works completely when the Async method is applied (on the button press), the method starts leaving the main "waiting" part without any part of the autoloaded method, and finally leaves directly at the end.

The entire process begins with this method:

  Private Waste Startbatch () {var batch size = (int) batchasislider.value; If (_config.AutoStart) {ExecutionLogAddItem (string.Format ("Auto Batch Processing started (batch size: {0})", batchies.tostring ()); Work.factor Startup (async () = & gt; {wait for batch transfer; fullbatch ();}); } And {var start = ConfirmStartBatch (); DoBatch = start.ContinueWith (async (continuity) = & gt; {start the work // if (start.Result == true) {ExecutionLogAddItem ("started batch processing."); ExecutionLogAddItem (String.Format ("batch BatchTransfer (batchSize), set to size {0} ", batchSize.ToString ()); .continueWith ((x) => CompleteBatch ());} else {ExecutionLogAddItem (" batch processing aborted ") ;}}}); }}  

If the _config.AutoStart is true, the BatchTransfer method does not seem to do anything, instead the program leaves directly to the CompleteBatch ( ) The method works as expected to apply everything manually.

Strange, I set a breakpoint on the wait BatchTransfer (batchSize) in the autostarted method, I can move through the code and transfer the batch . So when this debugging works, it does not happen while not debugging. Please help!

The reason for this is -

  StartNew (async () = & gt; {wait for batch transfer; fullbatch ();});  

You are waiting to complete the internal work with waiting but task.finter. Start (async () = & gt; .

  Wait Task.Factory -  In this way, there is an asynchronous task and not waiting you too! Code> Task.Factory.StartNew (async () = & gt; When you want to debug, a separate thread that is calling the internal work and you can see the execution but when running normally So the background is still working, but you can not see it because you have the  symbol for the job If you check the thread pool and thread ID, I'm sure you will see that when they debug it, they were not doing it. 

This blog can help you understand the situation -


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -