gpt4 book ai didi

c# - 后台 worker 不工作 WPF

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:38 24 4
gpt4 key购买 nike

在我的 WPF 程序中,它花费了大量的处理时间并卡住了很长时间。

所以我决定使用后台 worker 并在后台处理它。

但它不起作用。通过调试,程序停止在Render3D()。它不会抛出异常。就像你输入 return 一样。

换句话说,它在到达 Render3D() 后什么都不做,只会返回。

(我没有说它会返回因为我不确定但是行为与返回相同)

    private readonly BackgroundWorker backgroundWorker = new BackgroundWorker();
private AssetDeclaration _assetDeclaration = new AssetDeclaration();

public MainWindow()
{
backgroundWorker.DoWork += backgroundWorker1_DoWork;
backgroundWorker.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
InitializeComponent();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 1000; i++)
{
if (!((BackgroundWorker)sender).CancellationPending)
{
Render3D(); // will return at this point. (why?) or waiting for something to start?
((BackgroundWorker)sender).ReportProgress(i);
}
else
{
e.Cancel = true;
break;
}
}
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Done!");//will show message box instant.
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ProgressBar1.Value = e.ProgressPercentage;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
//...Some work here before starting Hard job!
//...From now i want to start heavy process in background.
//...with report to progress bar at same time.
backgroundWorker.RunWorkerAsync(100);
}

Render3D() 在没有后台处理的情况下工作正常,但会卡住一段时间。

Render3D() 在 MainWindowPartial 类中。因为有很多方法,所以我决定将它们分开。

还有我如何在 backgroundWorker1_DoWork 之外使用 ReportProgress 。例如在 Render3D() 中。

最后一件事:我想知道如何向用户显示完成了多少进程。

已解决!

问题是因为我在 Render3D() 中设置了 Viewport3D

我将它从 Render3D 中分离出来,问题得到解决。感谢 Henk Holterman 提供正确答案。

似乎有些任务不能在另一个线程中完成。通过错误报告,我发现无效任务正在设置 Viewport3D 属性。

此任务必须在主线程中完成。

下面是使后台工作程序停止运行的无效代码。

DefineCamera();
Viewport.Children.Add(model); // Must be run in Main thread.

还有这部分。

    private void DefineCamera()
{
PerspectiveCamera camera = new PerspectiveCamera
{
FieldOfView = 60
};

PositionCamera(camera);
Viewport.Camera = camera; // Must be run in Main thread.
}

最佳答案

首先,您很难找到错误。

... the program stop at Render3D(). It does not throw exception. Its like when you put return.

实际发生的是您的方法抛出异常并被 Backgroundworker 捕获。它被转移到 Completed 事件,但您必须在那里对其进行操作。

private void worker_Completed(object sender, RunWorkerCompletedEventArgs e) 
{
// check error, check cancel, then use result
if (e.Error != null)
{
// handle the error
}
else if (e.Cancelled)
{
// handle cancellation
}
else
{
// use the result(s) on the UI thread
}
// general cleanup
}

未能查看 e.Errore.Result 等同于在您的程序中有一个空的 catch{} block .

有了错误处理,我们就有了

oh yes it shown Error. System.InvalidOperationException the calling thread cannot access this object because a different thread owns it

这表明您的 Render3D() 仍在某处与 GUI 交互。

基本建议是将所有计算(以及 I/O、数据库)工作与 UI 工作分开。您可以在线程中运行受 CPU 限制和 I/O 限制的代码,但 GUI 是单线程的,您只能从主线程与其交互。

关于c# - 后台 worker 不工作 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30698429/

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