Asynchronously ????? C# ?????? ?? ??? ???? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 315582 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

Microsoft .NET Framework asynchronously ?????? ?? ??? ???? ?? ??? ???? ????? ??? ?????? ?? ????????? ???? ?? ??? ?????? ?? ???????? ????? ?? ????????? ??? ?? ???? ????? ???? ?? ??? ?????? ?????? ?????? ?? ???? asynchronously ?????? ?? ??? ????? ???? ??????? (?????????) ?????? ??? ??? ?????? ?? ????????? ???? ?? ??? ??? ??? ?? ???? ????? ??? ?????? ?????? ??? ????? ???? ?? ??? ????????? ???? ?? ?? ??? ???? ?? ???? ??? ?? ?????? ??????? ???? ??? Contrast ??, ?? ?? ??? ?????????? ??? ???? ?? ?? ??????? ???????? ?? ??? ??? ?????????? ??? ?? ??? ?? ???? ????????? ???? ?? ?? Visual C# ?? ????? ?? ??? ???? ?? ??? ???? ?????

??????????


????? ???? outlines ???????? ?????????, ??????????, ??????? ?????? ?? ?????? ??? ?? ???? ???:
  • Microsoft Windows 2000 ?? Microsoft Windows XP ?? Microsoft Windows Server 2003
  • ????? Studio .NET ?? Visual Studio 2005
?? ???? ????? ?? ?? ?? ????? ???? ?? ?????? ???:
  • ?????? ??????? ??? ????? C#
  • ???????????? ?? ????? ???? ????

?????????? ??? ????? ???? ????

?????????? ????? ?? ???????????? ?? ????? ???? ??? ??? ?? ???? ?? ????????? ??? ??? ?????? ?????? ?? ?? ???? ???????? ??? ???????????? ???? ????????? ?????? ?? ?????? ?? ?? asynchronously wrapped ?????? ?? ??? ???? ?? ??? ??????? ?????? ????? ??? ?? ??????? ???BeginInvoke(), ??EndInvoke(). The parameter lists of these methods vary depending on the signature of the function that the delegate wraps. Note that the Visual Studio .NET IntelliSense feature does not displayBeginInvoke(), ??EndInvoke(), so you do not see them appear in the function lists as you type.

BeginInvoke()is used to initiate the asynchronous call. It has the same parameters as the wrapped function, plus two additional parameters that will be described later in this article.BeginInvoke()returns immediately and does not wait for the asynchronous call to complete.BeginInvoke()???? ?? ??IAsyncResult???????? ???

TheEndInvoke()?????? ?? ??? ?????????? ?? ???????? ?? ??????? ???? ?? ??? ????? ???? ???? ??? ?? ?? ??? ???? ?? ??? ??? ?? ???? ???BeginInvoke(). ??? ?????????? ??? ??? ??, ????? ???? ???EndInvoke()??? ???? ???? ?? ?????? ???????? ??EndInvoke()?????? ????? ???? ?????, ??Ref???????? wrapped ?????? ??, ??IAsyncResult???????? ?????? ???? ??? ??BeginInvoke().

????? ??? ?? ????????? ?? ?? ?????? ?? ?? ????BeginInvoke(), ??EndInvoke()????:
// The following delegate 
delegate string MethodDelegate(int iCallTime, out int iExecThread)  ;

// will have the following BeginInvoke() and EndInvoke methods:
IAsyncResult MethodDelegate.BeginInvoke(int iCallTime, out int iExecThread, AsyncCallback cb, object AsyncState); 

string MethodDelegate.EndInvoke (out int iExecThread, IAsyncResult ar) ;
				
?? ????? ???? ?? ??? ??? ??????? ????? ???BeginInvoke(), ??EndInvoke()?????????? ????? ?? ???? ?? after ???BeginInvoke(), ?? ?? ???? ???:
  • ???????? ??? ?? ??? ????? ???? ??? ?? ??? ?? ????? ????EndInvoke().
  • ??????? ???WaitHandle?????? ???? ??? ??IAsyncResult????????, ?? ????? ????WaitOne?? ?? ??????? ???? ?? ??? ????WaitHandle?????? ??, ?? ???? ??? ???EndInvoke().
  • PollIAsyncResult?? ?????????? ??? ????? ?? ???? ?? ?? ????????? ???? ?? ??? ????????, ?? ???? ??? ???EndInvoke().
  • ???? ?????? ????????? ?????? ??????? ??? ?????? ??? ?? ?????? ??????? ???EndInvoke()?? ??????????? ?????????? ??? ???? ???? ?? ??? ???? ?? ?????? ???
????? ??? ????? ?? ???????? ?? ??? ???? ?? ????? ?????? ?? ????? ?? ???? ????????? ??? ?? ????? ?? ??? contrast demonstrate:
string LongRunningMethod (int iCallTime, out int iExecThread)
{
	Thread.Sleep (iCallTime) ;
	iExecThread = AppDomain.GetCurrentThreadId ();
	return "MyCallTime was " + iCallTime.ToString() ;
}

				
LongRunningMethod()simulates ??? ?????? ?? ???? ??? ?? sleeping ?????? ????? ???? ??? ?? ????? ?? ??? ?? ??? ????????? ???? ?? ?? ????? ?? ID ????? ??? ?? ??? ??? asynchronously, ?? ??? executing ????? ?? ????? ID ?? ?????? ????? ?? ????? ???

????????? ?????? ?????? ?? ?? ?? ????????? ???? ?? ??? ???? ??? ??:
delegate string MethodDelegate(int iCallTime, out int iExecThread)  ;
				

????? 1: ??? ??? ???? Synchronously

????????? ???? ?? ?? ????? ?? ??? ???? ?? ??? ???? ????LongRunningMethod()synchronously ?? ????? ?? ????MethodDelegate????????? ??? ????? ?? asynchronously ????? ?? ???? ?? contrast ???
  1. Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005 ??????? ?????
  2. ??? ??? ????? C# ????? ????????? ????????? AsyncDemo ??? ??????
  3. ????????? ??? ??? ?? .cs ????? ?? ??? ?? ??? ??? ???????? AsyncDemo ??? ?? ?? ????? ??????:
    using System;
    using System.Threading ; 
    using System.Windows.Forms ;
    
    public class AsyncDemo
    {
    	string LongRunningMethod (int iCallTime, out int iExecThread)
    	{
    		Thread.Sleep (iCallTime) ;
    		iExecThread = AppDomain.GetCurrentThreadId ();
    		return "MyCallTime was " + iCallTime.ToString() ;
    	}
    
    	delegate string MethodDelegate(int iCallTime, out int iExecThread)  ;
    
    	public void DemoSyncCall()
    	{
    		string s ;
    		int iExecThread;
    
    		// Create an instance of a delegate that wraps LongRunningMethod.
    		MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;  
    			
    		// Call LongRunningMethod using the delegate.
    		s = dlgt(3000, out iExecThread);  
    			
    		MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", 
                                            and the thread ID {1}", s, iExecThread.ToString() ) );
    	}
    }
    					
    ????????? ???? ?? ??? ???, ?? ???? ?? ?????????? ??? ???? ?? ??? ???? ????? ???????? ????, ??? ??, ???? ?????DemoSyncCall()????, ?? synchronously ????????? ?? ??? ???? ?? ??? ???? ????????? ???? ?? ???
  4. ?? ????? ??? ??? ????? ??? ????????????????? ?? ???? ????????? ??? Visual Studio ???? ?? ????? ??:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoSyncCall() ;
    }
    					
  5. ???? ????????? ?? ????? ?? ??? CTRL + F5 ??????

????? 2: ??? ??? ???? Asynchronously ?? ????? ??EndInvoke()???????? ?? ??? ????

?? ?????? ??? ????? invokes ??? ???? asynchronously. ??? ???? ?? ??? ????? ???? ???? ?? ?? ??? ???????? ??BeginInvoke, do some work on the main thread, and then callEndInvoke(). ?? ???EndInvoke()does not return until the asynchronous call has completed. This call pattern is useful when you want to have the calling thread do work at the same time that the asynchronous call is executing. Having work occur at the same time can improve the performance of many applications. Common tasks to run asynchronously in this way are file or network operations.
  1. Add a method namedDemoEndInvoke()???? ?? ???AsyncDemo????? TheDemoEndInvokefunction demonstrates how to call the delegate asynchronously.
    public void DemoEndInvoke()
    {
    	MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
    	string s ;
    	int iExecThread;
    
    	// Initiate the asynchronous call.
    	IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);  
    
    	// Do some useful work here. This would be work you want to have
    	// run at the same time as the asynchronous call.
    
    	// Retrieve the results of the asynchronous call.
    	s = dlgt.EndInvoke (out iExecThread, ar) ;  
    
    	MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", 
                                   and the number {1}", s, iExecThread.ToString() ) );
    }
    					
  2. Edit the source code for?????so that it contains the following code:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoEndInvoke() ;
    }
    					
  3. Press CTRL+F5 to run your application.

Sample 3: Calling A Method Asynchronously And Using A WaitHandle To Wait For The Call To Complete


In this section, the sample calls the method asynchronously and waits for aWaitHandlebefore it callsEndInvoke(). TheIAsyncResultthat is returned byBeginInvoke()has anAsyncWaitHandle???? This property returns aWaitHandlethat is signaled when the asynchronous call completes. Waiting on aWaitHandleis a common thread synchronization technique. The calling thread waits on theWaitHandleby using theWaitOne()?? ??? ????WaitHandle.WaitOne()blocks until theWaitHandleis signaled. ??WaitOne()returns, you can do some additional work before you callEndInvoke(). As in the previous sample, this technique is useful for executing file or network operations that would otherwise block the calling main thread.
  1. Add a function namedDemoWaitHandle()???? ?? ???AsyncDemo????? TheDemoWaitHandle()function demonstrates how to call the delegate asynchronously.
    public void DemoWaitHandle ()
    {
    	string s ;
    	int iExecThread;
    
    	MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
    
    	// Initiate the asynchronous call.
    	IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null); 
    
    	// Do some useful work here. This would be work you want to have
    	// run at the same time as the asynchronous call.
    
    	// Wait for the WaitHandle to become signaled.
    	ar.AsyncWaitHandle.WaitOne() ;
    
    	// Get the results of the asynchronous call.
    	s = dlgt.EndInvoke (out iExecThread, ar) ;
    			
    	MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", 
                                     and the number {1}", s, iExecThread.ToString() ) );
    }
    					
  2. Edit the source code for?????so that it contains the following code:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoWaitHandle () ;
    }
    					
  3. Press CTRL+F5 to run your application.

Sample 4: Calling A Method Asynchronously by Using the Polling Call Pattern

In this section, the sample polls theIAsyncResultobject to find out when the asynchronous call has completed. TheIAsyncResultobject that is returned byBeginInvoke()has anIsCompletedproperty that returnsTrue?????????? ??? ????? ???? ??? ???? ??? ??? ?? ???? ???EndInvoke(). ?? ??? ???????? ???? ????????? ???? ????? ??? ?? ???? ???? ?????? ??? ?????? ????? ???? ??? ?? ?? ??? ??? ????? ???? ?? ??? ?????? ??? ?? Microsoft Windows ?????????, ?? ?? ?? ?????? ??? Windows ????????? ?? ????? ????? ??? ?????????? ??? ????????? ???? ?? ?? ?????????? ????? ?? ????? ???? ?? ??? ???? ?? ???? ???? ?? ???-??? ?? ???? ????IsCompleted????? ?? ??? ??? ??? ????? ?? ???? ??? ?? ???EndInvoke??IsCompleted???? ??True. ???????EndInvoke()?????????? ???????? ????? ??, ?? ?? ?? ??? ??????? ????????? ???? ??? ??? ?? knows ?? ???????? ????? ???
  1. Add a function namedDemoPolling()???? ?? ???AsyncDemo????? TheDemoPolling()function demonstrates how to call the delegate asynchronously and use polling to see if the process is complete.
    public void DemoPolling()
    {
    	MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
    	string s ;
    	int iExecThread;
    
    	// Initiate the asynchronous call.
    	IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null); 
    
    	// Poll IAsyncResult.IsCompleted
    	while(ar.IsCompleted == false)
    	{
    		Thread.Sleep (10) ;  // pretend to so some useful work
    	}
    	s = dlgt.EndInvoke (out iExecThread, ar) ;
    
    	MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", 
                                    and the number {1}", s, iExecThread.ToString() ) );
    }
    
    					
  2. Edit the source code for?????. Replace the content of the function with the following code:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoPolling () ;
    }
    					
  3. ???? ????????? ?? ????? ?? ??? CTRL + F5 ??????

Sample 5: Executing a Callback When an Asynchronous Method Completes

In this section, the sample provides a callback delegate to theBeginInvoke()function that the system executes when the asynchronous call completes. The callback callsEndInvoke()and processes the results of the asynchronous call. This call pattern is useful if the thread that initiates the asynchronous call does not need to process the results of the call. The system invokes the callback on a thread other than the initiating thread when the asynchronous call completes.

To use this call pattern, you must pass a delegate of typeAsyncCallbackas the second-to-last parameter of theBeginInvoke()?????? ?? ??? ???? ???..BeginInvoke()also has a final parameter of type????????into which you can pass any object. This object is available to your callback function when it is invoked. One important use for this parameter is to pass the delegate that is used to initiate the call. The callback function can then use theEndInvoke()function of that delegate to complete the call. This call pattern is demonstrated below.
  1. Add a two methods namedDemoCallback(), ??MyAsyncCallback()???? ?? ???AsyncDemo????? TheDemoCallback()method demonstrates how to call the delegate asynchronously. It uses a delegate to wrap theMyAsyncCallback()method, which the system calls when the asynchronous operation completes.MyAsyncCallback()callsEndInvoke().
    public void DemoCallback()
    {
    	MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
    	string s ;
    	int iExecThread;
    
    	// Create the callback delegate.
    	AsyncCallback cb = new AsyncCallback(MyAsyncCallback);
    
    	// Initiate the Asynchronous call passing in the callback delegate
    	// and the delegate object used to initiate the call.
    	IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, cb, dlgt); 
    }
    
    public void MyAsyncCallback(IAsyncResult ar)
    {
    	string s ;
    	int iExecThread ;
    
    	// Because you passed your original delegate in the asyncState parameter
    	// of the Begin call, you can get it back here to complete the call.
    	MethodDelegate dlgt = (MethodDelegate) ar.AsyncState;
    
    	// Complete the call.
    	s = dlgt.EndInvoke (out iExecThread, ar) ;
    
    	MessageBox.Show (string.Format ("The delegate call returned the string:   \"{0}\", 
                                    and the number {1}", s, iExecThread.ToString() ) );
    }
    
    					
  2. Edit the source code for?????. Replace the content of the function with the following code:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoCallback() ;
    }
    					
  3. ???? ????????? ?? ????? ?? ??? CTRL + F5 ??????

???

???? ID: 315582 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
??????: 
kbhowtomaster kbmt KB315582 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:315582

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com