Using System; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Text; Using System.Threading; Using System.Windows.Forms; Namespace CW { public partial class Asyncdemo:form { Public Asyncdemo () { InitializeComponent (); } private void Delgate_load (object sender, EventArgs e) { } <summary>
Ways to implement delegates
</summary>
<param name= "Icalltime" ></param>
<param name= "Iexecthread" ></param>
<returns></returns>
String Longrunningmethod (int icalltime, out int iexecthread)
{
Thread.Sleep (Icalltime);
Iexecthread = Appdomain.getcurrentthreadid ();
Return "Mycalltime is" + icalltime.tostring ();
} Delegate string methoddelegate (int icalltime, out int iexecthread); #region Example 1: Synchronous Calling Method #region Example 1: Synchronous calling method /**/ /* * Synchronous Call Methods * */ /**/ ///<summary> ///Example 1: Synchronous call method ///</summary> public void Demosynccall () { string s; int iexecthread; Create an instance of the 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 ())); } #endregion #region Example 2: Calling a method asynchronously by EndInvoke ()
/**/
/*
* Using call mode is to invoke BeginInvoke, do some processing of the main thread, and invoke EndInvoke ().
* Note that EndInvoke () does not return until the asynchronous call has completed.
* This call pattern is useful when a calling thread is executing an asynchronous call while working.
* Having concurrent work can improve the performance of many applications.
* Common tasks run asynchronously in this way are file or network operations.
* */
/**/
<summary>
Example 2: Calling a method asynchronously via EndInvoke () call mode
</summary>
public void Demoendinvoke ()
{
Methoddelegate dlgt = new Methoddelegate (this. Longrunningmethod);
string S;
int iexecthread; Initiate the asynchronous call. IAsyncResult ar = dlgt. BeginInvoke (5000, out iexecthread, NULL, NULL); Do some useful work. This would is 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 ())); } #endregion #region Example 3: Calling the method asynchronously and using A WaitHandle to wait for the call to complete
/**/
/*
* Returned by BeginInvoke () IAsyncResult has a AsyncWaitHandle property.
* This property returns WaitHandle after the asynchronous call completes, the notification is. Waiting for WaitHandle is a common thread synchronization technique.
* Through the WaitHandle WaitOne () method calls the thread to wait on the WaitHandle.
* Until notification is WaitHandle WaitOne () block. When WaitOne () returns, you do some extra work before calling EndInvoke ().
* For performing file or network operations, the calling mainline Cheng is blocked, which is useful in previous examples.
* */
/**/
<summary>
Example 3: Calling the method asynchronously and using A WaitHandle to wait for the call to complete
</summary>
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. This would is 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 ())); } #endregion #region Example 4: Calling a method asynchronously by polling the calling pattern
/**/
/*
* Returned by BeginInvoke () IAsyncResult object has a IsCompleted property returns True after an asynchronous call completes.
* Then you can call EndInvoke (). If your application is constantly working on a long term function call is useful for this calling pattern.
* MicrosoftWindows application is an example of this.
* The primary thread's Windows application can continue to process user input when executing an asynchronous call.
* It can periodically check whether the iscompleted to the call is complete. It calls EndInvoke when IsCompleted returns TRUE.
* until it knows that the operation is complete because EndInvoke () is blocked until the asynchronous operation is complete, the application does not call it.
* */
/**/
<summary>
Example 4: Calling the method asynchronously by polling the call mode
</summary>
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 ())); } #endregion #region Example 5: Performing a callback after the asynchronous method completes
/**/
/*
* In this section, the example provides the BeginInvoke () function, in which the system executes the callback delegate after the asynchronous call completes.
* Callback calls EndInvoke () and handles the results of the asynchronous call.
* It is useful to invoke this calling pattern if the asynchronous calling thread is not required to process the result.
* The system call thread is raised after the asynchronous call completes.
* If you use this invocation pattern, the arguments for the second to last-BeginInvoke () function must pass a AsyncCallback type delegate.
* BeginInvoke () also has the last parameter to type the object to you can put any object. When it calls this object can be used for your callback function.
* An important use for this parameter is to pass the delegate for initialization calls.
* The callback function is then used with the delegate EndInvoke () function to complete the call. This invocation pattern is shown in.
* */
/**/
<summary>
Example 5: Performing a callback after the asynchronous method completes
</summary>
public void Democallback ()
{
Methoddelegate dlgt = new Methoddelegate (this. Longrunningmethod);
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 (5000, 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, and you can get it back 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 ()) ); //console.writeline (string. Format ("The delegate call returned the string: " {0} ", and the number {1}", S, iexecthread.tostring ()); } #endregion private void Button1_Click (object sender, EventArgs e) { // Demosynccall (); //demoendinvoke (); //demowaithandle (); //demopolling (); democallback (); } } } |