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

 

经过改进后:

  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. { 
  9.     public class AsyncEvent 
  10.     { 
  11.         /// <summary> 
  12.         /// 声明委托 
  13.         /// </summary> 
  14.         /// <typeparam name="T"></typeparam> 
  15.         /// <returns></returns> 
  16.         public delegate T MyAsyncDelegate<T>(); 
  17.  
  18.         public delegate T MyAsyncDelegate2<T>(int a, int b);  
  19.  
  20.         /// <summary> 
  21.         /// 回调函数得到异步线程的返回结果 
  22.         /// </summary> 
  23.         /// <param name="iasync"></param> 
  24.         public static T CallBack<T>(IAsyncResult iasync) 
  25.         { 
  26.             AsyncResult async = (AsyncResult)iasync; 
  27.             MyAsyncDelegate<T> del = (MyAsyncDelegate<T>)async.AsyncDelegate; 
  28.             return (T)del.EndInvoke(iasync); 
  29.         } 
  30.     } 
  31. } 

 

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

 

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