- Lightweight threads that use delegates
- Asynchronous delegate
An easy way to create a thread is to define a delegate and invoke it asynchronously. A delegate is a type-safe reference to a method (type-safe code refers to a memory location that access is authorized to access. For example, type-safe code cannot read values from private fields of other objects. It only accesses the type from a well-defined allowed mode to read. Type-safe code has well-defined data types). The Delegate class also supports calling methods asynchronously, and in the background, the Delegate class creates a thread that executes the task. (PS: A delegate uses a thread pool to complete an asynchronous task.) After a delegate is defined, you can have different techniques to invoke the delegate asynchronously and return the result.
- Vote
One technique is to vote and check whether the delegate has completed its task. The delegate class you create provides the BeginInvoke () method, in which you can pass an input parameter defined with a delegate type. The BeginInvoke () method always has two additional parameters for the AsyncCallback and object types. The sample code is as follows:
The results of the operation are as follows:
. Takeawhile started!
.......................................... Takesawhile completed
Result:2
?
In addition to checking that the delegate is complete, you can invoke the EndInvoke () method of the delegate type after you have completed the work performed by the main thread. The EndInvoke () method waits until the delegate finishes its task position. However, if the principal thread ends without waiting for the delegate to complete its task before the end of the delegate, the delegate terminates. Examples are as follows:
???????? The results of the operation are as follows:
Main Thread Sleep ms
Takeawhile started!
Main Thread waiting for delegate
Takesawhile completed
Result:2
- Wait handle
Another way to wait for the result of a delegate is to apply a wait handle associated with the Iaayncresult. You can access the wait handle by using the AsyncWaitHandle property. This property returns an object of type WaitHandle, which can wait for the delegate thread to complete its task. WaitOne () takes a time-out as the first optional parameter. The sample code looks like this:
???????? The results are as follows:
Takeawhile started!
Takesawhile completed
Can Get result now
Result:2
Main Thread exit
???? As you can see, the main thread waits for 2100ms, so you can see the result of the asynchronous delegate, and if you set the main thread's wait time to less than 2000ms, you may not see the result of the asynchronous delegate. Of course, this is not necessarily, the key to see how the operating system is scheduled.
?
- Asynchronous callbacks
The third way to wait for the result of a delegate is to use an asynchronous callback. In the 3rd parameter of the Bengininvoke () method, you can pass a method that satisfies the requirements of the AsyncCallback delegate. For the last argument, you can pass an arbitrary object to access it from the callback method. Passing a delegate instance is useful so that the callback method can use it to get the results of an asynchronous method. For example, the results are as follows.
C # threads, Task Summary (i)