常用的三種非同步寫法
阿新 • • 發佈:2019-02-16
/*BackgroundWorker*/
1: using (var appinstruction = new BackgroundWorker())
{
appinstruction.DoWork += appinstruction_DoWork;
appinstruction.RunWorkerCompleted += appinstruction_RunWorkerCompleted;
appinstruction.RunWorkerAsync();
}
2://執行
void appinstruction_DoWork(object sender, DoWorkEventArgs e)
{
//程式碼
}
//完成
void appinstruction_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//程式碼
}
/*IAsyncResult*/
IAsyncResult result = this.BeginInvoke(new MethodInvoker(() =>
{
//程式碼
}));
result.AsyncWaitHandle.WaitOne(-1, false);
3/*BeginInvoke*/
this.BeginInvoke((MethodInvoker)delegate
{
//程式碼
});
1: using (var appinstruction = new BackgroundWorker())
{
appinstruction.DoWork += appinstruction_DoWork;
appinstruction.RunWorkerCompleted += appinstruction_RunWorkerCompleted;
appinstruction.RunWorkerAsync();
}
2://執行
void appinstruction_DoWork(object sender, DoWorkEventArgs e)
{
//程式碼
}
//完成
void appinstruction_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//程式碼
}
/*IAsyncResult*/
IAsyncResult result = this.BeginInvoke(new MethodInvoker(() =>
{
//程式碼
}));
result.AsyncWaitHandle.WaitOne(-1, false);
3/*BeginInvoke*/
this.BeginInvoke((MethodInvoker)delegate
{
//程式碼
});