In the past, in the programming, asynchronous use of relatively few, resulting in C # Some of the basics of things are not familiar with, often to use when to find data more passive, but not really inside understand, always feel not their knowledge (digression)
First delegate keyword Delegate
1) definition
The delegate must have the same signature as the Async method to invoke
Public delegate string Asyncmethodcaller (int callduration, out int threadId);
2. Methods for asynchronous delegates
static string TestMethod (int callduration, out int threadId)
{
Thread.Sleep (callduration);
ThreadId = Thread.CurrentThread.ManagedThreadId;
return string. Format ("My call time is {0}.", callduration.tostring ());
}
3) Call
Asyncmethodcaller caller = new Asyncmethodcaller (TestMethod);
IAsyncResult result = caller. BeginInvoke (10000, out threadId, NULL, NULL);
Call the EndInvoke method, wait for the asynchronous call to complete, and get the result.
String returnvalue = caller. EndInvoke (out threadId, result);
Note that there are several key words BeginInvoke, EndInvoke IAsyncResult
The BeginInvoke method triggers your async method, which has the same parameters as the Async method you want to execute. There are also two optional parameters, and the first is that the AsyncCallback delegate is the callback method that completes asynchronously. The second is a user-defined object that is passed to the callback method. BeginInvoke immediately returns and does not wait for the asynchronous call to complete (continue executing the code below, without waiting). BeginInvoke returns the IAsyncResult interface, which can be used to detect the asynchronous invocation process.
The result of an asynchronous call is detected by the EndInvoke method. If the asynchronous call has not yet completed, EndInvoke will block the calling thread until it finishes. The EndInvoke parameter includes the out and ref parameters.
Of course, you can Use the asynchronous detection without EndInvoke.
The second Kind
result . Asyncwaithandle.waitone (); Block current main thread know Async method Complete Callback Touch method
result . Asyncwaithandle.close (); Run out of close
The third method of
Define First
1) AutoResetEvent AutoResetEvent = new AutoResetEvent (false);
2) Set the signal waiting callback AutoResetEvent when the asynchronous method is completed. Set ();
3) Set the wait AutoResetEvent after the main thread calls the Async method. WaitOne ();
There is also a delegate usage is very convenient personal advice to use later this usage Action has parameters no method value Func has return value no parameter
Defined:
action<string, String> act = new action<string, string> (showstring);
Async method:
void showstring (String str, string yy)
{
Thread.Sleep (50000);
}
Defined
func<bool> methodcall = sendtofile;
Async methods
public bool Sendtofile ()
{
return true;
}
New features after using NET3.0 with this delegate later lambda expression code looks simple without too many definitions
For example
AutoResetEvent AutoResetEvent = new AutoResetEvent (false);
action<string, String> act = new action<string, string> ((o, p) = =
{
Thread.Sleep (5000);
AutoResetEvent. Set ();
});
Act. BeginInvoke ("One", "222", NULL, NULL);
AutoResetEvent. WaitOne ();
AutoResetEvent. Close ();
() = = an anonymous function,=> preceded by a parameter, followed by a function body. You can think of it as a function.
NET. Asynchronous delegate Knowledge