锁定调度员

发布于 2024-12-22 21:08:42 字数 307 浏览 2 评论 0原文

是否有必要锁定多个线程通过调度程序访问同一 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巷子口的你 2024-12-29 21:08:42

如果您随后要以同步方式编组到另一个线程,则不应获取锁 - 否则如果您尝试在另一个线程(本例中为调度程序线程)中获取相同的锁你最终会陷入僵局。

如果 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 using pbLadder.Value to execute occurred on a different thread is irrelevant.

花辞树 2024-12-29 21:08:42

Dispatcher 上执行的所有操作都会排队并在 UI 线程上按顺序执行。这意味着不会发生类似增量的数据竞争。 Invoke 方法本身是线程安全的,因此将操作添加到队列中也不需要任何锁定。

来自 MSDN

使用指定的参数执行指定的委托
在与 Dispatcher 关联的线程上同步。

和:

该操作被添加到 Dispatcher 的事件队列中
指定 DispatcherPriority。

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. The Invoke method itself is thread-safe, so also adding the action to the queue does not require any locking.

From MSDN:

Executes the specified delegate with the specified arguments
synchronously on the thread the Dispatcher is associated with.

and:

The operation is added to the event queue of the Dispatcher at the
specified DispatcherPriority.

追风人 2024-12-29 21:08:42

尽管这个已经很老了,但它位于我搜索结果的顶部,而且我还很新(毕业后 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文