并行事件更新 UI
我有一个用 C# 编写的类。在其中我想在列表上并行运行某个函数。在每个项目完成后,我想更新进度条。然而,我的程序出现了非常奇怪的行为。它执行事件并到达我的子进程,但从未继续实际执行任何代码。相反,它只是冻结了。 (我混合了 vb.net 和 c#。它将在某个时候被重写)
所以在我的 Windows 窗体中我调用
progressBar.Visible = True
progressBar.Value = 0
progressBar.Maximum = dataGrid.SelectedRows.Count
AddHandler quoteManager.refreshStarted, AddressOf progressBarCounter
quoteManager.refreshAllAsync(list)
并且事件很简单
Private Sub progressBarCounter()
Me.Invoke(Sub()
If progressBar.Value = progressBar.Maximum Then
progressBar.Visible = False
Else
progressBar.Value += 1
End If
End Sub)
End Sub
,并且在报价管理器类中我定义了这个。
public event Action refreshStarted;
public void refreshAllAsync(List<BindableQuote> bindableQuotes)
{
bindableQuotes.AsParallel()
.WithDegreeOfParallelism(10)
.ForAll((quote) => {
quote.refreshAll();
if (refreshStarted != null) { refreshStarted(); }
});
}
因此,出于某种原因,我让它在列表中的每个项目上输入progressBarCounter,但它从未存在。相反,它只是保持表单冻结。
I have a class written in C#. In it I want to run a certain function in parallel on a list. After it completes on each item I would like to update a progress bar. However, I get very odd behavior from my program. It executes the event and reaches my sub but never proceeds to actually execute any code. Instead it just freezes. (I've mixed vb.net and c#. It will be rewritten at some point)
so in my windows form I call
progressBar.Visible = True
progressBar.Value = 0
progressBar.Maximum = dataGrid.SelectedRows.Count
AddHandler quoteManager.refreshStarted, AddressOf progressBarCounter
quoteManager.refreshAllAsync(list)
and the event is simply
Private Sub progressBarCounter()
Me.Invoke(Sub()
If progressBar.Value = progressBar.Maximum Then
progressBar.Visible = False
Else
progressBar.Value += 1
End If
End Sub)
End Sub
and in the quote manager class I have this defined.
public event Action refreshStarted;
public void refreshAllAsync(List<BindableQuote> bindableQuotes)
{
bindableQuotes.AsParallel()
.WithDegreeOfParallelism(10)
.ForAll((quote) => {
quote.refreshAll();
if (refreshStarted != null) { refreshStarted(); }
});
}
So for some reason I get it to enter progressBarCounter on each item in the list but it never exists. Instead it just keeps the form frozen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定这是什么情况,但看起来 ProgressBarCounter 正在阻塞,因为您正在调用 Invoke。您应该改用 BeginInvoke 吗?使用 BeginInvoke 可能会解决死锁问题。请参阅这篇文章:Invoke() 和 BeginInvoke() 之间有什么区别
I am not exactly sure this is what is happening, but it looks like progressBarCounter is blocking because you are calling Invoke. Should you be using BeginInvoke instead? Using BeginInvoke might solve the deadlock issue. See this post: What's the difference between Invoke() and BeginInvoke()
这里发生的情况是您从多个线程访问 UI 对象。
这是不支持的。您必须在工作线程上运行此代码,并让它以某种方式积累进度,并将消息发送回 UI 线程。
BackgroundWorker
类可以帮助您实现返回 UI 线程的编组。What appears to be happening here is that you access UI objects from multiple threads.
That's not supported. You'll have to run this code on a worker thread, and let it somehow accumulate progress, and send messages back to the UI thread. The
BackgroundWorker
class can help you implement the marshalling back to the UI thread.