处置后台工作者不起作用 [英] Disposing background worker does not work

查看:75
本文介绍了处置后台工作者不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个后台工作者调用一个表格,拿着一个gif动画。目的是在进程正在进行时显示动画,但是在进程完成时它应该关闭。但即使在完成该过程后它也不会关闭。请帮忙。

谢谢



Hi,
I have a background worker that calls a form, holding a gif animation. The purpose is to display the animation while process is underway but it should close when the process is done. But it does not close even after completion of the process. Please help.
Thanks

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
       frmAnimation.ShowDialog()
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       BackgroundWorker1.RunWorkerAsync()
       Dim sqldatasourceenumerator1 As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance
       datatable1 = sqldatasourceenumerator1.GetDataSources()
       DataGridView1.DataSource = datatable1

       'I have tried CancelAsync, but did not work

       BackgroundWorker1.CancelAsync()
       frmAnimation.Dispose()
   End Sub

推荐答案


你能不能通过下面的例子。



Hi,
Can you please go through the below example.

Form2 myForm = new Form2();
bool bFormOpened = false;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker backgroundWorker = sender as BackgroundWorker;
    if (backgroundWorker != null)
    {
        while (!backgroundWorker.CancellationPending)
        {
            if (!bFormOpened)
            {
                myForm.Show();
                bFormOpened = true;

            }
        }

        // cancellation was processed
        if (backgroundWorker.CancellationPending)
        {
            myForm.Dispose();
            e.Cancel = true;
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void button3_Click(object sender, EventArgs e)
{

    backgroundWorker1.CancelAsync();
}





另外,请确保为backgroundWorker1设置以下两个属性为TRUE

- WorkerReportsProgress

- WorkersupportsCancellation



希望这对你有帮助!



最好的问候

Muthuraja



Also , make sure the below two property set as TRUE for backgroundWorker1
- WorkerReportsProgress
- WorkersupportsCancellation

Hope this helps this to you!

Best Regards
Muthuraja


这篇关于处置后台工作者不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆