BackGround工人问题

编程入门 行业动态 更新时间:2024-10-11 15:23:50
本文介绍了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网站上后台工作者的例子展示了如何创建一个可取消的后台工作者: msdn.microsoft/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: msdn.microsoft/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工人问题

本文发布于:2023-11-25 14:54:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工人   BackGround

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!