Summary of C asynchronous call instance

  • 2021-07-24 11:38:28
  • OfStack

This article illustrates the method of C # asynchronous invocation. Share it for your reference. The details are as follows:


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>
  ///  Methods for Implementing 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 was " + iCallTime.ToString();
  }
  delegate string MethodDelegate(int iCallTime, out int iExecThread);
  #region  Example  1 :   Invoke methods synchronously #region  Example  1 :   Invoke methods synchronously 
  /// <summary>
  ///  Example  1 :   Invoke methods synchronously 
  /// </summary>
  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() ) );
  }
  #endregion
  #region  Example  2 :   Pass  EndInvoke()  Invoke mode to call methods asynchronously 
  /// <summary>
  ///  Example  2 :   Pass  EndInvoke()  Invoke mode to call methods asynchronously   
  /// </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 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() ) );
  }
  #endregion
  #region  Example  3 :   Invoke methods asynchronously and use the  A WaitHandle  To wait for the call to complete 
  /// <summary>
  ///  Example  3 :   Invoke methods asynchronously and use the  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 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() ) );
  }
  #endregion
  #region  Example  4 :   Asynchronous invocation method calls mode by polling 
  /// <summary>
  ///  Example  4 :   Asynchronous invocation method calls mode by polling 
  /// </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 :   Execute callback after asynchronous method completes 
  /// <summary>
  ///  Example  5 :   Execute callback after 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, 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()));
   //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();
  }
 }
}

I hope this article is helpful to everyone's C # programming.


Related articles: