Wait for the asynchronous Delegate of the handle judgment to be completed

Source: Internet
Author: User

Use the AsyncWatiHandle attribute to access the waiting handle. The WaitOne () method blocks the current thread until the asynchronous call thread finishes returning the available handle before executing the current thread.

Program:

[Html]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading;
 
Namespace AsyncDelegate
{
Class Program
{
Public delegate int TakeSomeTimeDelegate (int data, int MS );
 
Static void Main (string [] args)
{
TakeSomeTimeDelegate dl = TakeSomeTime;
IAsyncResult ar = dl. BeginInvoke (1,200, null, null );
 
While (true)
{
Console. WriteLine (".");
Console. WriteLine ("Run in thread:" + Thread. CurrentThread. ManagedThreadId );
If (ar. AsyncWaitHandle. WaitOne (100, false ))
{
Console. WriteLine ("Can get the result now ");
Break;
}
}
 
// While (! Ar. IsCompleted)
//{
// Console. WriteLine (".");
// Console. WriteLine ("Run in thread:" + Thread. CurrentThread. ManagedThreadId );
// Thread. Sleep (50 );
//}
 
Int result = dl. EndInvoke (ar );
Console. WriteLine ("Result: {0}", result );
}
 
Static int TakeSomeTime (int data, int MS)
{
Console. WriteLine ("TakeSomeTime started! ");
Console. WriteLine ("Run in thread:" + Thread. CurrentThread. ManagedThreadId );
Thread. Sleep (MS );
Console. WriteLine ("TakeSomeTime Completed! ");
Return ++ data;
}
}
}
[Html]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading;
 
Namespace AsyncDelegate
{
Class Program
{
Public delegate int TakeSomeTimeDelegate (int data, int MS );
 
Static void Main (string [] args)
{
TakeSomeTimeDelegate dl = TakeSomeTime;
IAsyncResult ar = dl. BeginInvoke (1,200, null, null );
 
While (true)
{
Console. WriteLine (".");
Console. WriteLine ("Run in thread:" + Thread. CurrentThread. ManagedThreadId );
If (ar. AsyncWaitHandle. WaitOne (100, false ))
{
Console. WriteLine ("Can get the result now ");
Break;
}
}
 
// While (! Ar. IsCompleted)
//{
// Console. WriteLine (".");
// Console. WriteLine ("Run in thread:" + Thread. CurrentThread. ManagedThreadId );
// Thread. Sleep (50 );
//}
 
Int result = dl. EndInvoke (ar );
Console. WriteLine ("Result: {0}", result );
}
 
Static int TakeSomeTime (int data, int MS)
{
Console. WriteLine ("TakeSomeTime started! ");
Console. WriteLine ("Run in thread:" + Thread. CurrentThread. ManagedThreadId );
Thread. Sleep (MS );
Console. WriteLine ("TakeSomeTime Completed! ");
Return ++ data;
}
}
}

Running result:

 

 

 

Ar. AsyncWaitHandle. WaitOne () blocks the current thread and executes the current thread again after the asynchronous call thread completes obtaining the available handle.

Author: xuhongwei0411

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.