Monday, 15 February 2010

c# - Task Parallel Library - Task.Delay() usage -


I am after some interpretation of what is happening.

  // version: Async private Async zero DoWorkAsync () {stopwatch STPWatch = new stopwatch (); _logger.WriteToLog ("Something is working ... etc ... + ThreadID =" + thread. Current documentaries managed); StpWatch.Start (); Awaiting work Daley (5000); StpWatch.Stop (); _logger.WriteToLog ("ThreadID =" + Finished the work on the thread, currently refred. ManagedTrade ID + "Elapsed time:" + stpWatch.ElapsedMilliseconds); }  

Called as:

  // Loop 10 times parallel. For (1, 11, p = & gt; DoWorkAsync ());  

So far the work is delayed by approximately 5 seconds as expected, (although for parallel, before the loop is completed, it is expected.).

However if I write the sync version of the above method:

  // VERSION: Synchronous Private Zero DoWork () {stopwatch STPWatch = new stopwatch (); _logger.WriteToLog ("Something is working ... etc ... + ThreadID =" + thread. Current documentaries managed); StpWatch.Start (); Task.Delay (5000); StpWatch.Stop (); _logger.WriteToLog ("ThreadID =" + Finished the work on the thread, currently refred. ManagedTrade ID + "Elapsed time:" + stpWatch.ElapsedMilliseconds); }  

i.e. I have removed async / deleted keywords, and call like this

  parallel Pho (1, 11, p => douwer ());  

, there is no delay of 5 seconds as before and the tasks are completed almost immediately (as verified by my log file).

Tasks. There is no effect in the case after the Della statement, can not I use it without the keyword?

Some TPL gurus are hopeful of what is happening!

Tasks. There is no effect in the case after the Della statement, should I use it without the keyword?

OK, you can not use it without doing something Task. Delay returns a task that will complete later. It does all this, it is not in itself - anything "current" for the current thread. Schedule a callback to execute the rest of the async method using awaiting , when that task is complete, and gives control to the caller after scheduling the callback.

If you are looking for the function.delee , this is basically the thread. Sleep .


No comments:

Post a Comment