锁定调度员
是否有必要锁定多个线程通过调度程序访问同一 wpf 组件的代码片段?
示例:
void ladder_OnIndexCompleted(object sender, EventArgs args)
{
lock (locker)
{
pbLadder.Dispatcher.Invoke(new Action(() => { pbLadder.Value++; }));
}
}
pbLadder
是一个进度条,该事件可以同时从多个线程引发。
Is it necessary to lock code snippet where multiple threads access same wpf component via dispatcher?
Example:
void ladder_OnIndexCompleted(object sender, EventArgs args)
{
lock (locker)
{
pbLadder.Dispatcher.Invoke(new Action(() => { pbLadder.Value++; }));
}
}
pbLadder
is a progress bar and this event can be raised from multiple threads in the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您随后要以同步方式编组到另一个线程,则不应获取锁 - 否则如果您尝试在另一个线程(本例中为调度程序线程)中获取相同的锁你最终会陷入僵局。
如果
pbLadder.Value
仅在 UI 线程中使用,那么您无需担心线程安全的锁定 - 所有操作都发生在同一个线程上,这一事实将您与许多操作隔离开来。常见的多线程问题。事实上,导致使用pbLadder.Value
执行代码的原始操作发生在不同的线程上,这一事实是无关紧要的。You should not acquire a lock if you're then going to marshal to another thread in a synchronous fashion - otherwise if you try to acquire the same lock in the other thread (the dispatcher thread in this case) you'll end up with a deadlock.
If
pbLadder.Value
is only used from the UI thread, then you don't need to worry about locking for thread safety - the fact that all the actions occur on the same thread isolates you from a lot of the normal multi-threading problems. The fact that the original action which caused the code usingpbLadder.Value
to execute occurred on a different thread is irrelevant.在
Dispatcher
上执行的所有操作都会排队并在 UI 线程上按顺序执行。这意味着不会发生类似增量的数据竞争。Invoke
方法本身是线程安全的,因此将操作添加到队列中也不需要任何锁定。来自 MSDN:
和:
All actions executed on the
Dispatcher
are queued up and executed in sequence on the UI thread. This means that data races like that increment cannot occur. TheInvoke
method itself is thread-safe, so also adding the action to the queue does not require any locking.From MSDN:
and:
尽管这个已经很老了,但它位于我搜索结果的顶部,而且我还很新(毕业后 4 个月了),所以在阅读了其他人的评论后,我去和我的高级编码员交谈。上面其他人说的是准确的,但我觉得答案没有提供解决方案,只是提供信息。以下是我的高级编码员的反馈:
“确实,调度程序在自己的线程上运行,但如果另一个线程正在访问调度程序想要访问的对象,则在调度程序等待访问时,所有 UI 处理都会停止。理想情况下,您希望复制调度程序需要访问的对象并将其传递给调度程序,然后调度程序可以自由编辑该对象,而不必等待其他线程释放其锁。 ”
干杯!
Even though this one is pretty old it was at the top of my search results and I'm pretty new (4 months since I graduated) so after reading other peoples comments, I went and spoke with my senior coder. What the others are saying above is accurate but I felt the answers didn't provide a solution, just information. Here's the feedback from my senior coder:
"It's true that the Dispatcher is running on its own thread, but if another thread is accessing an object that the dispatcher wants to access then all UI processing stops while the dispatcher waits for the access. To solve this ideally, you want to make a copy of the object that the dispatcher needs to access and pass that to the dispatcher, then the dispatcher is free to edit the object and won't have to wait on the other thread to release their lock."
Cheers!