gpt4 book ai didi

c# - BackgroundWorker 不会在 CancelAsync() 上停止并且只工作一次

转载 作者:行者123 更新时间:2023-11-30 16:05:40 25 4
gpt4 key购买 nike

我有一种叫做 Sorter 的表格。上面有“jademy”按钮,可以打开“Progress Window”窗口

private void jademy_Click(object sender, EventArgs e)
{
ProgressWindow progress = new ProgressWindow();
progress.ShowDialog();
}

“进度窗口”表单的代码如下:

public partial class ProgressWindow : Form
{
private BackgroundWorker backgroundWorker = new BackgroundWorker();

public ProgressWindow()
{
InitializeComponent();
stop.Visible = true;
ok.Visible = false;
backgroundWorker.RunWorkerAsync();
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.WorkerSupportsCancellation = true;

#region block1

backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);

#endregion
}

private void stop_Click(object sender, EventArgs e)
{
backgroundWorker.CancelAsync();
}

private void ok_Click(object sender, EventArgs e)
{
this.Close();
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
backgroundWorker.ReportProgress(i);
}

}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
this.Text = "Done: " + e.ProgressPercentage.ToString() + "%";
}

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
MessageBox.Show("Cancelled", "Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
}

else if (!(e.Error == null))
{
MessageBox.Show("Error: " + e.Error.Message, "ERROR!", MessageBoxButtons.OKCancel);
}
else
{
ok.Visible = true;
stop.Visible = false;
}
}
}

现在。我有三个问题。

  1. 点击停止按钮不执行任何操作。 “backgroundWorker.CancelAsync()”似乎不会停止进程。

  2. 当我关闭进度窗口并想再次运行它时,我必须等待一段时间才能单击“jademy”按钮。否则进度窗口显示如下: enter image description here (没有任何变化)而不是这个:enter image description here看起来程序“记得”工作已经完成,即使它是 ProgressWindow 的新实例。请注意,在不正确的版本上,“确定”按钮立即可见 - 而不是等待工作完成。

  3. 我想澄清“ block 1”中的代码。老实说,我并不完全理解。这部分真的必不可少吗?我的意思是,我发现了很多示例(也在这个论坛上 - 例如 here ),其中没有包括这部分,用户报告该解决方案有效。就我而言,没有这部分进度条根本不起作用,但也许我做错了什么。

最佳答案

  1. 调用 CancelAsync 会停止所有挂起的工作。但如果工作已经开始,则方法体需要检查是否调用了取消。参见 CancelAsync

CancelAsync submits a request to terminate the pending background operation and sets the CancellationPending property to true.

When you call CancelAsync, your worker method has an opportunity to stop its execution and exit. The worker code should periodically check the CancellationPending property to see if it has been set to true.

  1. 我对此一无所知。顺便说一下,图像不起作用。将其嵌入问题中。
  2. 代码分配一个在 BackgroundWorker 启动时执行的方法,您连接方法以报告进度并在后台工作完成后进行清理/更新。

关于c# - BackgroundWorker 不会在 CancelAsync() 上停止并且只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33191015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com