|
Speed (Fastest 1) |
return value |
Multi-parameter |
Waiting to be completed within the time limit |
End after timeout |
Threadpool.unsafequeueuserworkitem () |
1 |
Non-native support 1 |
Non-native support |
Non-native support 3 |
Not supported |
ThreadPool.QueueUserWorkItem () |
2.7 |
Non-native support 1 |
Non-native support |
Non-native support 3 |
Not supported |
Task () |
4.5 |
Support 2 |
Non-native support |
Support |
Voluntary end |
Delegate.beininvoke () |
25.4 |
Non-native support 1 |
Support |
Support 4 |
Not supported |
Thread.Start () |
11009 |
Non-native support 1 |
Non-native support |
Non-native support 3 |
Support |
such as Threadpool.unsafequeueuserworkitem (() =>result=add (());
With task<>
Inside at the end of the program eventwaithandle.set (), Outside WaitOne (TimeSpan).
Get The return value of BeginInvoke AsyncResult, then AsyncResult.AsyncWaitHandle.WaitOne ();
There is a picture of the truth. This is the time required for various asynchronous methods to loop n times.
The code is as follows:
Static voidMain (string[] args) {Action ThreadStart= (() = { }); WaitCallback WaitCallback=NewWaitCallback (A = { }); Stopwatch Stopwatch=NewStopwatch (); Stopwatch.reset (); Stopwatch.start (); for(inti =0; I <10000; i++) {System.Threading.ThreadPool.UnsafeQueueUserWorkItem (WaitCallback,NULL); } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","Threadpool.unsafequeueuserworkitem ():", stopwatch.elapsedticks); Gc. Collect (); Stopwatch.reset (); Stopwatch.start (); for(inti =0; I <10000; i++) {System.Threading.ThreadPool.QueueUserWorkItem (waitcallback); } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","ThreadPool.QueueUserWorkItem ():", stopwatch.elapsedticks); Gc. Collect (); Stopwatch.reset (); Stopwatch.start (); for(inti =0; I <10000; i++) {Task T=NewTask (ThreadStart); T.start (); } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","Task ():", stopwatch.elapsedticks); Gc. Collect (); Stopwatch.reset (); Stopwatch.start (); for(inti =0; I <10000; i++) {Threadstart.begininvoke (NULL,NULL); } stopwatch.stop (); Console.WriteLine ("{0,-40}{1}","Delegate.beininvoke ():", stopwatch.elapsedticks); }
Note that the usage of the above BeginInvoke is not complete and you should call EndInvoke again. But given that BeginInvoke is the slowest, the EndInvoke will not be added.
So, if there is no need to return a value, generally use ThreadPool bar, to more control, the task. I can't think of the time to use BeginInvoke.
Difference and speed comparison of three asynchronous methods of BeginInvoke, ThreadPool and task