BackGround工人问题 [英] BackGround Worker Issue

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

问题描述

大家好!



这是我对后台工作者实施的第一次尝试。我试图通过单击按钮取消后台工作人员事件。目前的活动是do_work背景事件的取消永远不会发生,但它会继续下一个事件,只要隐藏表格并打开新表格。



有人可以说明我做错了什么或者我错过了能够取消点击按钮的后台活动吗?



Hi everyone!

This is my first attempt on a background worker implementation. I am trying to cancel the background worker events on a button click. The current activity is that the cancellation of the do_work background events never happens but it goes on to the next events as far as hiding the form and opening the new one.

Can someone shed some light as to what I'm doing wrong or what I'm missing to be able to cancel the background activities on button click?

bool networkStatus = NetworkInterface.GetIsNetworkAvailable();
private bool m_Cancel = false;
public bool Cancelled { get; set; }

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
     if (networkStatus == true)
     {
          outsetUpdates();                
     }
     else
     {
          this.Visible = false;
          LauncherForm lForm = new LauncherForm();
          lForm.Show();
          return;
     }

     if(this.Cancel)
     {
          MessageBox.Show("Update cancelled.");
          this.Visible = false;
          e.Cancel = true;
          LauncherForm mainForm = new LauncherForm();
          mainForm.Show();
          return;
     }
}

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

private void skipToFormButton_Click(object sender, EventArgs e)
{
     this.Cancelled = true;

     this.Hide();
     LauncherForm mainForm = new LauncherForm();
     this.m_Cancel = true;
     mainForm.Show();
     return;
}

推荐答案

问题1)你在哪里实例化backgroundWorker1?

问题2)你在后台工作者上设置了WorkerSupportsCancellation = true吗?



MSDN网站上后台工作者的例子展示了如何创建一个可取消的后台工作者:



http://msdn.microsoft.com/en-us /library/4852et58.aspx [ ^ ]
Question 1) Where do you instantiate the backgroundWorker1 ?
Question 2) Do you set WorkerSupportsCancellation = true on the background worker?

The example on the MSDN website for background worker shows how to create a cancellable background worker:

http://msdn.microsoft.com/en-us/library/4852et58.aspx[^]


如果你想取消它,也许你应该设置和测试相同的值?

If you want to cancel it, perhaps you should be setting and testing the same values?
    this.Cancelled = true;
...
    this.m_Cancel = true;




if(this.Cancel)


这篇关于BackGround工人问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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