gpt4 book ai didi

c# - 后台 worker 多次运行 doWork

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

private void button_uploadToPI_Click(object sender, RoutedEventArgs e)
{


this.newFilePath = this.textbox_input_filePath.Text;
this.label_status.Content = "";
if (bgWorker.IsBusy != true)
{


bgWorker.RunWorkerAsync();
}
}

这是我的点击事件。

public MainWindow()
{

InitializeComponent();

this.progressBar.Minimum = 0;
this.progressBar.Maximum = 100;

this.bgWorker = new BackgroundWorker();

this.bgWorker.WorkerReportsProgress = true;
this.bgWorker.WorkerSupportsCancellation = true;
reader.IsOpen = false;

this.bgWorker.DoWork += bgWorker_DoWork;
this.bgWorker.ProgressChanged += bgWorker_ProgressChanged;
this.bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
}

主窗口方法。

public partial class MainWindow : Window
{

private BackgroundWorker bgWorker;

bgWorker 在这里初始化。

void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

this.progressBar.Value = 0;
this.label_progressPercentage.Content = "0%";
reportTextBox.ScrollToEnd();
}

运行工作完成代码

主要问题是,如果我运行应用程序一次,它会按预期工作。如果我再次单击,它将执行该方法两次,如果我再次单击它,它将执行该方法 3 次。每次新点击都会增加。

谁能看到这里的主要问题?

最佳答案

我根据@galenus 的建议处理的示例。我和后台工作人员有同样的情况,点击工作按钮多次触发工作,直到我用按钮初始化工作人员。代码块:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;
using System.Data;

namespace MouseClick
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{



public MainWindow()
{
InitializeComponent();
}

public BackgroundWorker worker = new BackgroundWorker();

private void Button_Click(object sender, RoutedEventArgs e)
{
//e.Handled = true; // Din't do anything
worker = new BackgroundWorker(); //stopped the dowork being executed multiple times when button pressed again in the same session
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync();

}

void worker_DoWork(object sender, DoWorkEventArgs e)
{


int tot = 1;

MessageBox.Show(tot.ToString()); //if this message box appears multiple times, it means the dowork is executing multiple times

DataTable dt = new DataTable();

dt.Columns.Add("Running Number");

for (int i = 0; i <= 100; i++)
{
if (worker.CancellationPending == true)
{
//http://stackoverflow.com/questions/8300799/cancel-background-worker-exception-in-e-result
// e.Cancel = true; //This does the trick
e.Result = 100;
return;
}
worker.ReportProgress(i);
System.Threading.Thread.Sleep(1);
dt.Rows.Add(i);
}
e.Result = dt;
}

#region "worker_ProgressChanged"
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{

}
#endregion

#region "worker_RunWorkerCompleted"
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
myGrid.ItemsSource = ((DataTable)e.Result).DefaultView;
worker.Dispose();

}
#endregion

}
}

关于c# - 后台 worker 多次运行 doWork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27016324/

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