关于如何使用后台工作人员,我查看了msdn和堆栈交换的类似问题。
基本上,我的函数上传程序完成了实际工作,但我想要一个线程来更改 ui 的元素(进度条等),并在我发送事件以更改进度时。我尝试过的如下(经过严格编辑),它不起作用,并且在调用 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}
然后做的工作,它似乎永远不会到达这里。
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.