我已經看過msdn以及有關如何使用後臺工作人員的類似堆棧交換問題。基本上,我的函數上傳程序做了實際的工作,但我想要一個線程來改變它的元素(進度條等),當我發送事件的進度改變。我試過的是以下(嚴厲編輯),它不起作用,並且在調用runworkerasync之後該程序似乎被切斷。是否有一些簡單的錯誤或將我的命令發送到另一個線程是錯誤的?另一個後臺工作人員
BackgroundWorker backgroundUpload = new System.ComponentModel.BackgroundWorker();
第一位是呼叫:
if (backgroundUpload.IsBusy != true)
{
backgroundUpload.RunWorkerAsync(work);
// a command here for debug purposes (eg a message box) will run
}
else
{ //it doesn't go here, this isn't the error}
然後DoWork的,它似乎永遠不會來到這裏。
private void backgroundUpload_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = UploadProgram((Workload)e.Argument, worker, e); //workload is one of my enums
}
似乎從來沒有到過這裏。
bool UploadProgram(Workload work, BackgroundWorker worker, DoWorkEventArgs e)
{
}
//also there is progress changed and run worker complete.
我猜你的電話,但你必須在DoWork的事件委託的減速?即backgroundUpload.DoWork + = 新的DoWorkEventHandler(UploadProgram); –
那很簡單。幹得好,每個人都有不同的感受。 –