????? ??????? ????? Visual C# ???? ??? ??????

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

?? ??? ??????

??????

???? Microsoft .NET Framework ?? ??????? ????? ???? ??? ??????. ??????? ??????? ???? ??? ?????? ???? ?????? ????? ??? ?? ??????? ??? ???? ????? ????? ????? ????? ??????? ?????? ???? ??????. ?? ??????? ???? ?????? (??????) ? ??? ????? ?????? ????? ??? ??? ???? ??????? ????? ????????. ??????? ???? ????? ?????? ?????? ??????? ????? ??????? ??? ????????. ???? ?????? ??? ????? ??????? ??? ?????? ??????? ????? ????????? ??? ?????? ??????. ???? ??? ?????? ????? ?????? ???? ???????? Visual C#.

???????


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

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

??? ????? ??????????? ??? ????????? ???????? ????????. ????? ????? ?? ???? ?????? ????. ???????? ???? ???? ?????? ????? ????? ?????? ??????? ?????? ??????? ???? ??? ??????. ?? ??? ???????? BeginInvoke() ? EndInvoke(). ????? ????? ??????? ?? ??? ????? ????? ??? ????? ???? ???? ??????. ???? ?? Visual Studio ?? ???? ???? IntelliSense .NET BeginInvoke() ? EndInvoke() ? ??? ?? ??? ??? ??? ???? ?? ?????? ???? ????? ???????.

??? ??????? BeginInvoke() ???? ????????? ??? ??????. ???? ?? ??? ????????? ???? ?????? ??????? ???? ??????? ?? ?????? ?????? ???? ???? ?? ???? ?????? ?? ??? ???????. ????? ?????? BeginInvoke() ? ?? ????? ????????? ??? ?????? ??????. BeginInvoke() ????? ???? IAsyncResult.

??? ??????? ?????? EndInvoke() ???????? ????? ????????? ??? ??????. ???? ??????? ?? ??? ??? BeginInvoke(). ?? ???? ????????? ??? ?????? ?? ????? ??? ? EndInvoke() ??? ??? ?????. ????? ?????? ?????? EndInvoke()out ? ???????? ref ?????? ??????? ???? ???? 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() ????? ??????????? ??? ?????????. ??? ??????? BeginInvoke() ?????:
  • ????????? ???? ??? ???? ?? ?? ???????? EndInvoke().
  • ?????? ??? WaitHandle ??????? ?? ??? ???? IAsyncResult ?????? ??????? WaitOne ????? ??? ???? ??? ??????? WaitHandle ?? ?? ???????? EndInvoke().
  • ????????? ???? IAsyncResult ?????? ?? ??? ?????? ????????? ??? ?????? ?? ?? ???????? EndInvoke().
  • ???? ??? ?????? ??????? ????? ?? ????? ???? ??????. ??? ?????? ?? ??????? ?????? EndInvoke() ????????? ????? ????????? ??? ?????? ??? ?????.
????? ????????? ???????? ??????? ??? ??? ???????? ?????? ? ??????? ?? ????? ??????? ?????? ???????? ?????? ???????:
string LongRunningMethod (int iCallTime, out int iExecThread)
{
	Thread.Sleep (iCallTime) ;
	iExecThread = AppDomain.GetCurrentThreadId ();
	return "MyCallTime was " + iCallTime.ToString() ;
}

				
LongRunningMethod() ????? ???? ???? ??? ??????? ????? ????? ?????? ????. ???? ?????? ??? ?????? "?" ????? ???? ??????? ???? ???? ?????? ???. ???? ??????? ?? ???? ??? ?????? ? ??? ?? ????? ???? ??????? ???? ????? ??????? ????? ???? ???? ????? ???????.

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

????? 1: ????? ??????? ???? ??????

???? ??? ??????? ????? ??????? LongRunningMethod() ???? ?????? ???????? ???? MethodDelegate. ????? ???? ????? ??? ?? ???? ????? ????????? ???? ??? ??????.
  1. ??? ????? Microsoft Visual Studio .NET ?? Microsoft Visual Studio 2005.
  2. ????? ????? ??????? Visual C# ?? ???? ?????? ???? ???? AsyncDemo.
  3. ????? ??? ????? AsyncDemo ????? ??? ??? ??? ??????? ?? ??? .cs ????:
    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() ???? ???? ????? ??????? ?????? ???? ??????.
  4. ?? ?????? ????????? ???????? ??????? ?? ?? ???? ??????? Visual Studio ???? ???? ?????? ?? ??????? ??????:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoSyncCall() ;
    }
    					
  5. ???? CTRL + F5 ?????? ??????? ????? ??.

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

?? ??? ?????? ?????? ??????? ????? ???? ???? ??? ??????. ??? ??????? ???????? ??????? BeginInvoke, ??? ????? ??? ???? ??????? ??????? ?? ?? ???????? EndInvoke(). ?????? ?? ????? EndInvoke() ??? ?? ????? ????????? ??? ??????. ??? ?????? ??????? ?????? ????? ???? ?? ???? ????? ??????? ??? ?? ??? ????? ????? ????????? ??? ??????. ???? ?? ????? ????? ???? ?? ??? ????? ??? ????? ???? ?????? ?? ?????????. ?????? ??????? ??????? ???? ??? ?????? ???? ??????? ???? ?????? ?????? ?? ?????.
  1. ????? ????? ???? DemoEndInvoke() ??? ??? AsyncDemo. ???? DemoEndInvoke ???? ????? ??????? ?????? ???? ??? ??????.
    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. ????? ????????? ???????? ?????? ?? ??????? ???? ????? ??? ????????? ???????? ???????:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoEndInvoke() ;
    }
    					
  3. ???? CTRL + F5 ?????? ??????? ????? ??.

????? 3: ????? ????? ???? ??? ?????? ? ??????? WaitHandle ??? ?????? ?????? ???????? ??? ??????


?? ??? ?????? ?????? ??????? ??????? ???? ??? ?????? ?????? WaitHandle ??? ?????? EndInvoke(). IAsyncResult ???? ?? ??????? ?????? BeginInvoke() ????? ??? ????? AsyncWaitHandle. ????? ??? ??????? WaitHandle ??????? ??? ????? ????????? ??? ??????. ?????? WaitHandle ????? ?????? ???? ????? ?????. ????? ???? ????? ??????? ??? WaitHandle ?????? ??????? ??????? WaitOne()WaitHandle. ??? WaitOne() ??? ??????? WaitHandle. ????? ???? WaitOne() ????? ?????? ??? ????? ?????? ??? ??????? EndInvoke(). ??? ?? ????? ??????? ??? ??????? ????? ????? ?????? ????? ?? ?????? ???? ??? ???? ????? ??????? ???????.
  1. ????? ???? ???? DemoWaitHandle() ??? ??? AsyncDemo. ???? DemoWaitHandle() ???? ????? ??????? ?????? ???? ??? ??????.
    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. ????? ????????? ???????? ?????? ?? ??????? ???? ????? ??? ????????? ???????? ???????:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoWaitHandle () ;
    }
    					
  3. ???? CTRL + F5 ?????? ??????? ????? ??.

????? 4: ????? ????? ???? ??? ?????? ???????? ??? ?????? ???????

?? ??? ?????? ?????? ?????? ?? ???? IAsyncResult ?????? ??? ?????? ????????? ??? ??????. ???? IAsyncResult ???? ?? ??????? ?????? BeginInvoke() ????? ??? ????? IsCompleted ?????? True ??? ????? ????????? ??? ??????. ?? ????? ??????? EndInvoke(). ???? ??? ?????? ???????? ??? ?? ??? ??????? ????? ?? ????? ??????? ???? ?? ???? ?? ???? ?????? ??????? ???? ??????? ????? ?????. ????? Microsoft Windows ???? ?? ???. ???? ?????? ???? ??????? ??????? ?? ??????? Windows ??????? ????? ???????? ????? ????? ??????? ??? ??????. ????? ???? ???? ?????? IsCompleted ?????? ?? ??? ?? ????? ?????????. ?????? EndInvoke ??? ????? IsCompletedTrue. ??? ???? EndInvoke() ??? ??? ?????? ??????? ??? ?????????, ??????? ?? ???????? ??? ??? ???? ?? ????? ???????.
  1. ????? ???? ???? DemoPolling() ??? ??? AsyncDemo. ???? DemoPolling() ???? ????? ??????? ?????? ???? ??? ?????? ?????????? ?????? ?????? ?????? ?? ??? ??? ???? ??????? ?????.
    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. ????? ????????? ???????? ?????? ?? ???????. ?????? ??????? ?????? ???????? ???????? ???????:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoPolling () ;
    }
    					
  3. ???? CTRL + F5 ?????? ??????? ????? ??.

????? 5: ??????? ??? ??????? ??? ?????? ????? ??????

?? ??? ?????? ???? ?????? ???? ?? ????? ??? ?????? BeginInvoke() ???? ???? ?????? ??? ????? ????????? ??? ??????. ?? ??????? ?????? EndInvoke() ????????? ????? ????????? ??? ??????. ??? ??????? ??? ?????? ??? ???? ??????? ???? ???? ????????? ??? ?????? ?? ????? ??? ?????? ????? ?????????. ??????? ?????? ?? ??????? ??? ???? ????? ??? ??? ???? ????? ????? ??? ????? ????????? ??? ??????.

???????? ??? ?????? ??????? ??? ?? ??? ????? ??? AsyncCallback ?????? ??????? ??????? ?????? BeginInvoke(). ??? BeginInvoke() ????? ????? ??????? ?? ???? ?????? ???? ????? ????? ?? ????. ????? ??? ?????? ????? ?? ??????? ????? ??? ???????. ??????? ?????? ???? ???? ??????? ?? ????? ?????? ????????? ?? ??? ????????. ???? ??? ??? ??????? ????? ?? ??????? ?????? EndInvoke() ???? ?????? ?????? ????????. ??? ?? ????? ??? ?????? ??????? ?????.
  1. ????? ?????? ????? ?????? DemoCallback() ? MyAsyncCallback() ??? ??? AsyncDemo. ????? DemoCallback() ???? ????? ??????? ?????? ???? ??? ??????. ??????? ???? ?????? ??????? MyAsyncCallback() ????????? ?????? ??? ?????? ??????? ??? ???????. ????????? MyAsyncCallback()EndInvoke().
    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. ????? ????????? ???????? ?????? ?? ???????. ?????? ??????? ?????? ???????? ???????? ???????:
    static void Main(string[] args)
    {
    	AsyncDemo ad = new AsyncDemo () ;
    	ad.DemoCallback() ;
    }
    					
  3. ???? CTRL + F5 ?????? ??????? ????? ??.

???????

???? ???????: 315582 - ????? ??? ??????: 20/?? ??????/1427 - ??????: 4.3
????? ???
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# 2005 Express Edition
????? ??????: 
kbmt kbhowtomaster KB315582 KbMtar
????? ????
???: ??? ????? ??? ?????? ???????? ?????? ????? ???? ????? ?????????? ????? ?? ????????? ?????? ????. ???? ???? ?????????? ???? ?? ???????? ???????? ?????? ????????? ????? ????????? ???????? ????? ???????? ?????? ?? ?????? ??? ?? ???????? ???????? ?? ????? ??????? ?????? ??? ??????? ?????? ??. ?????? ?? ???? ??? ??????? ???????? ????? ?? ???? ????? ?????? ??? ????? ??? ????? ??????? ?? ????? ?? ?????? ??? ??? ??????? ??????? ?? ????? ????? ????? ????? ?????. ?? ????? ???? ?????????? ??????? ??? ????? ?? ??????? ?? ????? ?????? ?? ??? ????? ?? ????? ??????? ?? ???????? ?? ??? ???????. ???? ???? ?????????? ???????? ??? ????? ?????? ??????? ??????
???? ??? ????? ??????? ?????? ??????????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