I 've been watching asynchronous programming for two days, and it's hard to understand. I don't know what I understand.
Public Delegate int taskwhiledelegate (INT data, int MS); // defines the delegate
// Delegate Method
Static int taskwhile (INT data, int MS)
{
Console. writeline ("taskwhilestart ");
Thread. Sleep (MS );
Console. writeline ("taskwhilecomplete ");
Return ++ data;
}
Static void main (string [] ARGs)
{
Taskwhiledelegate TD = new taskwhiledelegate (taskwhile );
Iasyncresult IAS = TD. begininvoke (1, 3000, null, null );
While (! IAS. iscompleted)
{
Console. writeline (".");
Thread. Sleep (50 );
}
Int result = TD. endinvoke (IAS );
Console. writeline ("Result: {0}", result );
Console. readkey ();
}
Do not know whether to understand or not. According to the output, the main function is the main process. First, an asynchronous delegate is created. To execute the taskwhile method.
Determine whether the enabled process is finished and the output is not completed. Then the main thread is sleeping for 50 ms, the asynchronous thread executes the output taskwhilestart, and sleep for 3000 Ms.
[] Asynchronous programming continues