C# winform中使用IAsyncResult实现异步编程

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Linq; 
  7. using System.Text; 
  8. using System.Windows.Forms; 
  9. using System.Runtime.Remoting.Messaging; 
  10.  
  11. namespace AsyncCall 
  12.     public partial class Form1 : Form 
  13.     { 
  14.         public Form1() 
  15.         { 
  16.             InitializeComponent(); 
  17.         } 
  18.  
  19.         private delegate int MyAsyncDelegate(); 
  20.  
  21.         private void button1_Click(object sender, EventArgs e) 
  22.         { 
  23.             this.progressBar1.Value = 0; 
  24.             this.progressBar1.Maximum = 1000000; 
  25.             this.progressBar1.Step = 10; 
  26.             MyAsyncDelegate del = new MyAsyncDelegate(DoAsyncEvent); 
  27.             IAsyncResult result = del.BeginInvoke(new AsyncCallback(CallBack), null); 
  28.         } 
  29.         /// <summary> 
  30.         /// 执行方法 
  31.         /// </summary> 
  32.         /// <returns></returns> 
  33.         private int DoAsyncEvent() 
  34.         { 
  35.             try 
  36.             { 
  37.                 while (this.progressBar1.Value < this.progressBar1.Maximum) 
  38.                 { 
  39.                     this.progressBar1.PerformStep(); 
  40.                 } 
  41.                 return 1; 
  42.             } 
  43.             catch 
  44.             { 
  45.                 return -1; 
  46.             } 
  47.         } 
  48.         /// <summary> 
  49.         /// 回调函数得到异步线程的返回结果 
  50.         /// </summary> 
  51.         /// <param name="iasync"></param> 
  52.         private void CallBack(IAsyncResult iasync) 
  53.         { 
  54.             AsyncResult async = (AsyncResult)iasync; 
  55.             MyAsyncDelegate del = (MyAsyncDelegate)async.AsyncDelegate; 
  56.             int iResult = del.EndInvoke(iasync); 
  57.             if (iResult>0) 
  58.             { 
  59.                 MessageBox.Show(string.Format("异步操作完成,返回结果为:{0}.",iResult)); 
  60.             } 
  61.         } 
  62.         /// <summary> 
  63.         /// 线程访问控件 
  64.         /// </summary> 
  65.         /// <param name="sender"></param> 
  66.         /// <param name="e"></param> 
  67.         private void Form1_Load(object sender, EventArgs e) 
  68.         { 
  69.             Control.CheckForIllegalCrossThreadCalls = false
  70.         } 
  71.  
  72.     } 

 

经过改进后:

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Runtime.Remoting.Messaging; 
  6.  
  7. namespace AsyncCall 
  8.     public class AsyncEvent 
  9.     { 
  10.         /// <summary> 
  11.         /// 声明委托 
  12.         /// </summary> 
  13.         /// <typeparam name="T"></typeparam> 
  14.         /// <returns></returns> 
  15.         public delegate T MyAsyncDelegate<T>(); 
  16.  
  17.         public delegate T MyAsyncDelegate2<T>(int a, int b);  
  18.  
  19.         /// <summary> 
  20.         /// 回调函数得到异步线程的返回结果 
  21.         /// </summary> 
  22.         /// <param name="iasync"></param> 
  23.         public static T CallBack<T>(IAsyncResult iasync) 
  24.         { 
  25.             AsyncResult async = (AsyncResult)iasync; 
  26.             MyAsyncDelegate<T> del = (MyAsyncDelegate<T>)async.AsyncDelegate; 
  27.             return (T)del.EndInvoke(iasync); 
  28.         } 
  29.     } 

 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Linq; 
  7. using System.Text; 
  8. using System.Windows.Forms; 
  9. using System.Runtime.Remoting.Messaging; 
  10.  
  11. namespace AsyncCall 
  12.     public partial class Form1 : Form 
  13.     { 
  14.         public Form1() 
  15.         { 
  16.             InitializeComponent(); 
  17.         } 
  18.  
  19.         private void button1_Click(object sender, EventArgs e) 
  20.         { 
  21.             this.progressBar1.Value = 0; 
  22.             this.progressBar1.Maximum = 1000000; 
  23.             this.progressBar1.Step = 10; 
  24.             AsyncEvent.MyAsyncDelegate<int> del = new AsyncEvent.MyAsyncDelegate<int>(DoAsyncEvent); 
  25.             IAsyncResult result = del.BeginInvoke(new AsyncCallback(CallBack), null); 
  26.         } 
  27.  
  28.         /// <summary> 
  29.         /// 回调函数得到异步线程的返回结果 
  30.         /// </summary> 
  31.         /// <param name="iasync"></param> 
  32.         public void  CallBack(IAsyncResult iasync) 
  33.         { 
  34.             if (AsyncEvent.CallBack<int>(iasync) > 0) 
  35.             { 
  36.                 MessageBox.Show("成功"); 
  37.             } 
  38.         } 
  39.  
  40.         /// <summary> 
  41.         /// 执行方法 
  42.         /// </summary> 
  43.         /// <returns></returns> 
  44.         private int DoAsyncEvent() 
  45.         { 
  46.             try 
  47.             { 
  48.                 while (this.progressBar1.Value < this.progressBar1.Maximum) 
  49.                 { 
  50.                     this.progressBar1.PerformStep(); 
  51.                 } 
  52.                 return 1; 
  53.             } 
  54.             catch 
  55.             { 
  56.                 return -1; 
  57.             } 
  58.         } 
  59.  
  60.         /// <summary> 
  61.         /// 线程访问控件 
  62.         /// </summary> 
  63.         /// <param name="sender"></param> 
  64.         /// <param name="e"></param> 
  65.         private void Form1_Load(object sender, EventArgs e) 
  66.         { 
  67.             Control.CheckForIllegalCrossThreadCalls = false
  68.         } 
  69.  
  70.     } 

 

posted @ 2011-08-17 01:32  therockthe  阅读(603)  评论(0)    收藏  举报