我的应用程序的用户将HTML类型转换为TextBox控件.
我希望我的应用程序在后台验证他们的输入.
因为我不想敲定验证服务,所以我尝试在每次验证之前建立一秒钟的延迟.
但是,我似乎无法正确中断已经运行的BackgroundWorker进程.
我的Visual Basic代码:
Sub W3CValidate(ByVal WholeDocumentText As String) 'stop any already-running validation If ValidationWorker.IsBusy Then ValidationWorker.CancelAsync() 'wait for it to become ready While ValidationWorker.IsBusy 'pause for one-hundredth of a second System.Threading.Thread.Sleep(New TimeSpan(0, 0, 0, 0, 10)) End While End If 'start validation Dim ValidationArgument As W3CValidator = New W3CValidator(WholeDocumentText) ValidationWorker.RunWorkerAsync(ValidationArgument) End Sub
看来在调用我的BackgroundWorker的CancelAsync()之后,它的IsBusy永远不会变为False.它陷入无限循环.
我究竟做错了什么?
尝试这样的事情:
bool restartWorker = false; void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // add other code here if (e.Cancelled && restartWorker) { restartWorker = false; backgroundWorker1.RunWorkerAsync(); } } private void button1_Click(object sender, EventArgs e) { if (backgroundWorker1.IsBusy) { restartWorker = true; backgroundWorker1.CancelAsync(); } else backgroundWorker1.RunWorkerAsync(); }