using system;
using system.componentmodel;
using system.windows.forms;
namespace backgroundworker元件
private int numbertocomputer = 0;
private int highpercentreched = 0;
private void background1_dowork(object sender, doworkeventargs e)
long computefibonacci(int n, backgroundworker worker, doworkeventargs e)
long result = 0;
if (worker.cancellationpending)
else
else
int percentcomplete = (int)((float)n / (float)numbertocomputer * 100);
if (percentcomplete > highpercentreched)
}return result;
}private void backgroundworker1_progresschange(object sender, progresschangedeventargs e)
/// /// 當輔助線程完成(無論是成功,失敗還是取消)時引發
///
///
///
private void backgroundworker1_runworkercompleted(object sender,runworkercompletedeventargs e)
else if (e.cancelled)
else
this.numericupdown1.enabled = true;
btnstart.enabled = true;
btncancel.enabled = false;
}/// /// 開始啟用非同步操作
///
///
///
private void startasyncbutton_click(object sender, eventargs e)
/// /// 請求取消後台掛起的操作
///
///
///
private void btncancel_click(object obj, eventargs e)
}}
關於BackgroundWorker的使用
dowork 呼叫 runworkerasync 時發生。progresschanged 呼叫 reportprogress 時發生。runworkercompleted 當後台操作已完成 被取消或引發異常時發生。先介紹一下backgroundworker的幾個屬性 方法 workerreports...
BackgroundWorker元件學習
今天看到別人的部落格中提到了backgroundworker元件。在現在的系統中有見到過這個元件,由於實際應用的系統中邏輯比較複雜所以也沒深入去看。今天湊巧看到了乙個關於backgroundworker元件的例子,對原 中存在的問題進行了一下修改。自己也學習了一下。using system usin...
使用BackgroundWorker元件
在應用程式中新增乙個backgroundworker例項,如果用的是vs,可以從工具上直接拖到應用程式 backgroundworker backgroundworker1 new backgroundworker 為了開始在後台操作,必須呼叫backgroundworker的runworkeras...