gpt4 book ai didi

c# - 使用BackgroundWorker先后完成两个方法WPF/C#

转载 作者:行者123 更新时间:2023-11-30 23:10:37 29 4
gpt4 key购买 nike

在我的程序中,我有两个方法需要一段时间才能完成,每个方法大约需要几分钟。在执行这些方法时,我在单独的窗口中显示一个进度条,显示每个方法的进度。我的两个方法都在静态 Utility 类中。它们如下所示:

public static class Utility
{
public static bool TimeConsumingMethodOne(object sender)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
(sender as BackgroundWorker).ReportProgress(i);
}
return true;
}

public static bool TimeConsumingMethodTwo(object sender)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(50);
(sender as BackgroundWorker).ReportProgress(i);
}
return true;
}
}

通过阅读 SO 中的类似问题,我了解到我应该使用 BackgroundWorker 并使用 RunWorkerCompleted() 来查看工作人员何时完成其工作。所以在我的 Main() 中,我使用了 BackgroundWorer() 并订阅了 RunWorkerCompleted() 方法。我的目标是首先运行 TimeConsumingMethodOne()(并在运行时显示进度),然后一旦完成,运行 TimeConsumingMethodTwo() 并再次显示进度,完成后输出消息框(模拟我程序中的其他一些工作) .我的 Main() 如下所示:

public partial class MainWindow : Window
{
public enum MethodType
{
One,
Two
}

private BackgroundWorker worker = null;
private AutoResetEvent _resetEventOne = new AutoResetEvent(false);
private AutoResetEvent _resetEventTwo = new AutoResetEvent(false);

private ProgressBarWindow pbWindowOne = null;
private ProgressBarWindow pbWindowTwo = null;

public MainWindow()
{
InitializeComponent();
}

private void btnRun_Click(object sender, RoutedEventArgs e)
{
RunMethodCallers(sender, MethodType.One);
_resetEventOne.WaitOne();
RunMethodCallers(sender, MethodType.Two);
_resetEventTwo.WaitOne();
MessageBox.Show("COMPLETED!");
}

private void RunMethodCallers(object sender, MethodType type)
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
switch (type)
{
case MethodType.One:
worker.DoWork += MethodOneCaller;
worker.ProgressChanged += worker_ProgressChangedOne;
worker.RunWorkerCompleted += worker_RunWorkerCompletedOne;
break;
case MethodType.Two:
worker.DoWork += MethodTwoCaller;
worker.ProgressChanged += worker_ProgressChangedTwo;
worker.RunWorkerCompleted += worker_RunWorkerCompletedTwo;
break;
}
worker.RunWorkerAsync();
}


private void MethodOneCaller(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
pbWindowOne = new ProgressBarWindow("Running Method One");
pbWindowOne.Owner = this;
pbWindowOne.Show();
});

Utility.TimeConsumingMethodOne(sender);
}

private void MethodTwoCaller(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
pbWindowTwo = new ProgressBarWindow("Running Method Two");
pbWindowTwo.Owner = this;
pbWindowTwo.Show();
});

Utility.TimeConsumingMethodTwo(sender);
}

private void worker_RunWorkerCompletedOne(object sender, RunWorkerCompletedEventArgs e)
{
_resetEventOne.Set();
}

private void worker_RunWorkerCompletedTwo(object sender, RunWorkerCompletedEventArgs e)
{
_resetEventTwo.Set();
}

private void worker_ProgressChangedOne(object sender, ProgressChangedEventArgs e)
{
pbWindowOne.SetProgressUpdate(e.ProgressPercentage);
}

private void worker_ProgressChangedTwo(object sender, ProgressChangedEventArgs e)
{
pbWindowTwo.SetProgressUpdate(e.ProgressPercentage);
}
}

现在我遇到的问题是,当我使用 _resetEventOne.WaitOne();用户界面挂起。如果我删除了这两个等待,这两个方法将异步运行,并且执行会继续并在这两个方法完成之前输出 MessageBox。

我做错了什么?如何让程序完成我的第一个 BackgroundWorker,然后转到下一个,然后在完成后输出 MessageBox?

最佳答案

Now the problem I have is, when I use _resetEventOne.WaitOne(); the UI hangs. If I removed those two waits, both methods run asynchronously and the execution moves on and outputs the MessageBox even before those two methods complete.

What am I doing wrong?

当您调用 WaitOne() 时,您正在阻塞 UI 线程,导致 UI 挂起。如果您删除该调用,那么您当然会立即启动两个工作人员。

有几种不同的方法可以解决您的问题。一种是尽可能地坚持您当前的实现,并且只修复最低限度的问题以使其正常工作。为此,您需要做的是执行 RunWorkerCompleted 中的实际下一条语句。处理程序,而不是使用事件来等待处理程序执行。

看起来像这样:

public partial class MainWindow : Window
{
public enum MethodType
{
One,
Two
}

private BackgroundWorker worker = null;

private ProgressBarWindow pbWindowOne = null;
private ProgressBarWindow pbWindowTwo = null;

public MainWindow()
{
InitializeComponent();
}

private void btnRun_Click(object sender, RoutedEventArgs e)
{
RunMethodCallers(sender, MethodType.One);
}

private void RunMethodCallers(object sender, MethodType type)
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
switch (type)
{
case MethodType.One:
worker.DoWork += MethodOneCaller;
worker.ProgressChanged += worker_ProgressChangedOne;
worker.RunWorkerCompleted += worker_RunWorkerCompletedOne;
break;
case MethodType.Two:
worker.DoWork += MethodTwoCaller;
worker.ProgressChanged += worker_ProgressChangedTwo;
worker.RunWorkerCompleted += worker_RunWorkerCompletedTwo;
break;
}
worker.RunWorkerAsync();
}

private void MethodOneCaller(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
pbWindowOne = new ProgressBarWindow("Running Method One");
pbWindowOne.Owner = this;
pbWindowOne.Show();
});

Utility.TimeConsumingMethodOne(sender);
}

private void MethodTwoCaller(object sender, DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
pbWindowTwo = new ProgressBarWindow("Running Method Two");
pbWindowTwo.Owner = this;
pbWindowTwo.Show();
});

Utility.TimeConsumingMethodTwo(sender);
}

private void worker_RunWorkerCompletedOne(object sender, RunWorkerCompletedEventArgs e)
{
RunMethodCallers(sender, MethodType.Two);
}

private void worker_RunWorkerCompletedTwo(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("COMPLETED!");
}

private void worker_ProgressChangedOne(object sender, ProgressChangedEventArgs e)
{
pbWindowOne.SetProgressUpdate(e.ProgressPercentage);
}

private void worker_ProgressChangedTwo(object sender, ProgressChangedEventArgs e)
{
pbWindowTwo.SetProgressUpdate(e.ProgressPercentage);
}
}

也就是说,BackgroundWorker已被更新的基于任务的 API 与 async 淘汰和 await .通过对您的代码进行一些小的更改,它可以适应使用较新的习惯用法:

public partial class MainWindow : Window
{
public enum MethodType
{
One,
Two
}

private ProgressBarWindow pbWindowOne = null;
private ProgressBarWindow pbWindowTwo = null;

public MainWindow()
{
InitializeComponent();
}

private async void btnRun_Click(object sender, RoutedEventArgs e)
{
await RunMethodCallers(sender, MethodType.One);
await RunMethodCallers(sender, MethodType.Two);
MessageBox.Show("COMPLETED!");
}

private async Task RunMethodCallers(object sender, MethodType type)
{
IProgress<int> progress;

switch (type)
{
case MethodType.One:
progress = new Progress<int>(i => pbWindowOne.SetProgressUpdate(i));
await Task.Run(() => MethodOneCaller(progress));
break;
case MethodType.Two:
progress = new Progress<int>(i => pbWindowTwo.SetProgressUpdate(i));
await Task.Run(() => MethodTwoCaller(progress));
break;
}
}

private void MethodOneCaller(IProgress<int> progress)
{
Dispatcher.Invoke(() =>
{
pbWindowOne = new ProgressBarWindow("Running Method One");
pbWindowOne.Owner = this;
pbWindowOne.Show();
});

Utility.TimeConsumingMethodOne(progress);
}

private void MethodTwoCaller(IProgress<int> progress)
{
Dispatcher.Invoke(() =>
{
pbWindowTwo = new ProgressBarWindow("Running Method Two");
pbWindowTwo.Owner = this;
pbWindowTwo.Show();
});

Utility.TimeConsumingMethodTwo(progress);
}
}

要执行上述操作,需要对 Utility 进行小幅调整。还有类:

static class Utility
{
public static bool TimeConsumingMethodOne(IProgress<int> progress)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
progress.Report(i);
}
return true;
}

public static bool TimeConsumingMethodTwo(IProgress<int> progress)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(50);
progress.Report(i);
}
return true;
}
}

也就是Progress<T>类取代了 BackgroundWorker.ProgressChanged事件和 ReportProgress()方法。

请注意,有了上面的代码,代码变得更短、更简单,并且以更直接的方式编写(即相关语句现在在同一个方法中彼此相连)。

你举的例子一定是简化了。这很好,但这确实意味着这里不知道 Thread.Sleep() 是什么。方法表示。事实上,在很多情况下,这种事情可以进一步重构,只异步完成长时间运行的工作。这有时可以进一步简化进度报告,因为它可以在 await 之后完成。 -ing 每个单独的异步执行的工作组件。

例如,假设循环中的工作本质上是异步的,或者成本足够高,因此使用 Task.Run() 是合理的。执行每个循环迭代。出于同样的目的,可以使用 Task.Delay() 表示:

static class Utility
{
public static async Task<bool> TimeConsumingMethodOne(Action<int> progress)
{
for (int i = 1; i <= 100; i++)
{
await Task.Delay(100);
progress(i);
}
return true;
}

public static async Task<bool> TimeConsumingMethodTwo(Action<int> progress)
{
for (int i = 1; i <= 100; i++)
{
await Task.Delay(50);
progress(i);
}
return true;
}
}

在上面,我也没有使用Progress<T> .只是一个简单的 Action<int>委托(delegate)调用者使用他们想要的方式。

随着这一改变,您的窗口代码变得更加简单:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private async void btnRun_Click(object sender, RoutedEventArgs e)
{
await MethodOneCaller();
await MethodTwoCaller();
MessageBox.Show("COMPLETED!");
}

private async Task MethodOneCaller()
{
ProgressBarWindow pbWindowOne =
new ProgressBarWindow("Running Method One") { Owner = this };
pbWindowOne.Show();

await Utility.TimeConsumingMethodOne(i => pbWindowOne.SetProgressUpdate(i));
}

private async Task MethodTwoCaller()
{
ProgressBarWindow pbWindowTwo =
new ProgressBarWindow("Running Method Two") { Owner = this };

pbWindowTwo.Show();

await Utility.TimeConsumingMethodTwo(i => pbWindowTwo.SetProgressUpdate(i));
}
}

当然,我借此机会删除了 MethodType枚举并直接调用方法,这进一步缩短了代码。但即使你所做的只是避免使用 Dispatcher.Invoke() ,这仍然大大简化了代码。

除此之外,如果您使用数据绑定(bind)来表示进度状态而不是直接设置值,WPF 会为您隐式处理跨线程调用,因此 Progress<T>即使您不能重构 Utility,也不需要类它本身的类代码是async .

但是,与离开 BackgroundWorker 相比,这些都是微小的改进。 .我建议这样做,但您是否投入时间进行这些进一步的改进并不重要。

关于c# - 使用BackgroundWorker先后完成两个方法WPF/C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45247256/

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