当一个方法的执行时间未知时,我们要在此方法执行完成后回调另外一个处理函数时,这段代码就有用处了
  1 using System;
using System;
2 using System.Drawing;
using System.Drawing;
3 using System.Collections;
using System.Collections;
4 using System.ComponentModel;
using System.ComponentModel;
5 using System.Windows.Forms;
using System.Windows.Forms;
6 using System.Data;
using System.Data;
7 namespace DelegateAndAsync
namespace DelegateAndAsync
8 {
{
9 /// <summary>
 /// <summary>
10 /// Form1 的摘要说明。
 /// Form1 的摘要说明。
11 /// </summary>
 /// </summary>
12 public class frmMain : System.Windows.Forms.Form
 public class frmMain : System.Windows.Forms.Form
13 {
 {
14
15 public delegate DateTime WorkHandler(int sleepMillisecond);
  public delegate DateTime WorkHandler(int sleepMillisecond);
16
17 private System.Windows.Forms.Button CmdStart;
  private System.Windows.Forms.Button CmdStart;
18 private System.Windows.Forms.RichTextBox RT001;
  private System.Windows.Forms.RichTextBox RT001;
19 private System.Windows.Forms.ProgressBar PB001;
  private System.Windows.Forms.ProgressBar PB001;
20 /// <summary>
  /// <summary>
21 /// 必需的设计器变量。
  /// 必需的设计器变量。
22 /// </summary>
  /// </summary>
23 private System.ComponentModel.Container components = null;
  private System.ComponentModel.Container components = null;
24
25 public frmMain()
  public frmMain()
26 {
  {
27 //
   //
28 // Windows 窗体设计器支持所必需的
   // Windows 窗体设计器支持所必需的
29 //
   //
30 InitializeComponent();
   InitializeComponent();
31
32 //
   //
33 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
34 //
   //
35 }
  }
36
37 /// <summary>
  /// <summary>
38 /// 清理所有正在使用的资源。
  /// 清理所有正在使用的资源。
39 /// </summary>
  /// </summary>
40 protected override void Dispose( bool disposing )
  protected override void Dispose( bool disposing )
41 {
  {
42 if( disposing )
   if( disposing )
43 {
   {
44 if (components != null)
    if (components != null) 
45 {
    {
46 components.Dispose();
     components.Dispose();
47 }
    }
48 }
   }
49 base.Dispose( disposing );
   base.Dispose( disposing );
50 }
  }
51
52 Windows 窗体设计器生成的代码
  Windows 窗体设计器生成的代码
100
101 /// <summary>
  /// <summary>
102 /// 应用程序的主入口点。
  /// 应用程序的主入口点。
103 /// </summary>
  /// </summary>
104 [STAThread]
  [STAThread]
105 static void Main()
  static void Main() 
106 {
  {
107 Application.Run(new frmMain());
   Application.Run(new frmMain());
108 }
  }
109
110 private void CmdStart_Click(object sender, System.EventArgs e)
  private void CmdStart_Click(object sender, System.EventArgs e)
111 {
  {
112 MessageBox.Show("Start");
   MessageBox.Show("Start");
113 WorkHandler wHandle = new WorkHandler(this.StartWork);
   WorkHandler wHandle = new WorkHandler(this.StartWork);
114 wHandle.BeginInvoke(10000,new AsyncCallback(this.CompleteWork),wHandle);
   wHandle.BeginInvoke(10000,new AsyncCallback(this.CompleteWork),wHandle);
115 
   
116 }
  }
117
118 /// <summary>
  /// <summary>
119 ///
  /// 
120 /// </summary>
  /// </summary>
121 /// <param name="sleepMillisecond"></param>
  /// <param name="sleepMillisecond"></param>
122 /// <returns></returns>
  /// <returns></returns>
123 private DateTime StartWork(int sleepMillisecond)
  private DateTime StartWork(int sleepMillisecond)
124 {
  {
125 this.CmdStart.Enabled = false;
   this.CmdStart.Enabled = false;
126 int val = 0;
   int val = 0;
127 while(true)
   while(true)
128 {
   {
129 val += 10/(sleepMillisecond/1000);
    val += 10/(sleepMillisecond/1000);
130 System.Threading.Thread.Sleep(1000/(sleepMillisecond/1000));
    System.Threading.Thread.Sleep(1000/(sleepMillisecond/1000));
131 this.PB001.Value = val;
    this.PB001.Value = val;
132 if(val == 100 || val > 100 )
    if(val == 100 || val > 100 )
133 break;
     break;
134
135 }
   }
136 return DateTime.Now;
   return DateTime.Now;
137 
  
138 }
  }
139
140 /// <summary>
  /// <summary>
141 /// 当StartWork完成后该做的任务
  /// 当StartWork完成后该做的任务
142 /// </summary>
  /// </summary>
143 /// <param name="ar"></param>
  /// <param name="ar"></param>
144 private void CompleteWork(IAsyncResult ar)
  private void CompleteWork(IAsyncResult ar)
145 {
  {
146 try
   try
147 {
   {
148 WorkHandler WH=(WorkHandler)ar.AsyncState;
    WorkHandler WH=(WorkHandler)ar.AsyncState;
149 DateTime dt = WH.EndInvoke(ar);
    DateTime dt = WH.EndInvoke(ar);   
150 MessageBox.Show(this,dt.ToString());
    MessageBox.Show(this,dt.ToString());
151 this.CmdStart.Enabled = true;
    this.CmdStart.Enabled = true;
152 }
   }
153 catch(Exception e)
   catch(Exception e)
154 {
   {
155 Console.Write(e);
    Console.Write(e);
156 }
   }
157 }
  }
158
159 private void CompleteWork()
  private void CompleteWork()
160 {
  {
161 
   
162 }
  }
163
164 }
 }
165 }
}
166
167
 using System;
using System;2
 using System.Drawing;
using System.Drawing;3
 using System.Collections;
using System.Collections;4
 using System.ComponentModel;
using System.ComponentModel;5
 using System.Windows.Forms;
using System.Windows.Forms;6
 using System.Data;
using System.Data;7
 namespace DelegateAndAsync
namespace DelegateAndAsync8
 {
{9
 /// <summary>
 /// <summary>10
 /// Form1 的摘要说明。
 /// Form1 的摘要说明。11
 /// </summary>
 /// </summary>12
 public class frmMain : System.Windows.Forms.Form
 public class frmMain : System.Windows.Forms.Form13
 {
 {14

15
 public delegate DateTime WorkHandler(int sleepMillisecond);
  public delegate DateTime WorkHandler(int sleepMillisecond);16

17
 private System.Windows.Forms.Button CmdStart;
  private System.Windows.Forms.Button CmdStart;18
 private System.Windows.Forms.RichTextBox RT001;
  private System.Windows.Forms.RichTextBox RT001;19
 private System.Windows.Forms.ProgressBar PB001;
  private System.Windows.Forms.ProgressBar PB001;20
 /// <summary>
  /// <summary>21
 /// 必需的设计器变量。
  /// 必需的设计器变量。22
 /// </summary>
  /// </summary>23
 private System.ComponentModel.Container components = null;
  private System.ComponentModel.Container components = null;24

25
 public frmMain()
  public frmMain()26
 {
  {27
 //
   //28
 // Windows 窗体设计器支持所必需的
   // Windows 窗体设计器支持所必需的29
 //
   //30
 InitializeComponent();
   InitializeComponent();31

32
 //
   //33
 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码34
 //
   //35
 }
  }36

37
 /// <summary>
  /// <summary>38
 /// 清理所有正在使用的资源。
  /// 清理所有正在使用的资源。39
 /// </summary>
  /// </summary>40
 protected override void Dispose( bool disposing )
  protected override void Dispose( bool disposing )41
 {
  {42
 if( disposing )
   if( disposing )43
 {
   {44
 if (components != null)
    if (components != null) 45
 {
    {46
 components.Dispose();
     components.Dispose();47
 }
    }48
 }
   }49
 base.Dispose( disposing );
   base.Dispose( disposing );50
 }
  }51

52
 Windows 窗体设计器生成的代码
  Windows 窗体设计器生成的代码100

101
 /// <summary>
  /// <summary>102
 /// 应用程序的主入口点。
  /// 应用程序的主入口点。103
 /// </summary>
  /// </summary>104
 [STAThread]
  [STAThread]105
 static void Main()
  static void Main() 106
 {
  {107
 Application.Run(new frmMain());
   Application.Run(new frmMain());108
 }
  }109

110
 private void CmdStart_Click(object sender, System.EventArgs e)
  private void CmdStart_Click(object sender, System.EventArgs e)111
 {
  {112
 MessageBox.Show("Start");
   MessageBox.Show("Start");113
 WorkHandler wHandle = new WorkHandler(this.StartWork);
   WorkHandler wHandle = new WorkHandler(this.StartWork);114
 wHandle.BeginInvoke(10000,new AsyncCallback(this.CompleteWork),wHandle);
   wHandle.BeginInvoke(10000,new AsyncCallback(this.CompleteWork),wHandle);115
 
   116
 }
  }117

118
 /// <summary>
  /// <summary>119
 ///
  /// 120
 /// </summary>
  /// </summary>121
 /// <param name="sleepMillisecond"></param>
  /// <param name="sleepMillisecond"></param>122
 /// <returns></returns>
  /// <returns></returns>123
 private DateTime StartWork(int sleepMillisecond)
  private DateTime StartWork(int sleepMillisecond)124
 {
  {125
 this.CmdStart.Enabled = false;
   this.CmdStart.Enabled = false;126
 int val = 0;
   int val = 0;127
 while(true)
   while(true)128
 {
   {129
 val += 10/(sleepMillisecond/1000);
    val += 10/(sleepMillisecond/1000);130
 System.Threading.Thread.Sleep(1000/(sleepMillisecond/1000));
    System.Threading.Thread.Sleep(1000/(sleepMillisecond/1000));131
 this.PB001.Value = val;
    this.PB001.Value = val;132
 if(val == 100 || val > 100 )
    if(val == 100 || val > 100 )133
 break;
     break;134

135
 }
   }136
 return DateTime.Now;
   return DateTime.Now;137
 
  138
 }
  }139

140
 /// <summary>
  /// <summary>141
 /// 当StartWork完成后该做的任务
  /// 当StartWork完成后该做的任务142
 /// </summary>
  /// </summary>143
 /// <param name="ar"></param>
  /// <param name="ar"></param>144
 private void CompleteWork(IAsyncResult ar)
  private void CompleteWork(IAsyncResult ar)145
 {
  {146
 try
   try147
 {
   {148
 WorkHandler WH=(WorkHandler)ar.AsyncState;
    WorkHandler WH=(WorkHandler)ar.AsyncState;149
 DateTime dt = WH.EndInvoke(ar);
    DateTime dt = WH.EndInvoke(ar);   150
 MessageBox.Show(this,dt.ToString());
    MessageBox.Show(this,dt.ToString());151
 this.CmdStart.Enabled = true;
    this.CmdStart.Enabled = true;152
 }
   }153
 catch(Exception e)
   catch(Exception e)154
 {
   {155
 Console.Write(e);
    Console.Write(e);156
 }
   }157
 }
  }158

159
 private void CompleteWork()
  private void CompleteWork()160
 {
  {161
 
   162
 }
  }163

164
 }
 }165
 }
}166

167

 
                    
                

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号