简体   繁体   中英

Stop backgroundworker from another backgroundworker c#

So i have 2 backgroundworkers in my Winform app, just call them b1 and b2. Here is my code:

private void b1_DoWork(object sender, DoWorkEventArgs e)
{
    b2.RunWorkerAsync();
    Thread.Sleep(3000);
    b2.CancelAsync();
}



private void b2_DoWork(object sender, DoWorkEventArgs e) 
{
    Global.GetPdfThumbnail(Global.inPDF, Global.outImage);
}

What i really want is run b2 thread and make b1 thread sleep for 3 seconds and then stop b2 thread.

The problem is after b2.CancelAsync(); instruction, b2 isBusy is still true.

Please help!

sorry about my english!

Put that to end of your b2 DoWork event:

while (b2.IsBusy)
{
    if (b2.CancellationPending)
    {
        e.Cancel = true;
        break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM