C# "Synchronization"中的 "SynchronizationContext"是什么意思?

标签 c# synchronizationcontext

我明白什么是同步上下文,但我就是不明白这个词中的“同步”是什么意思。你有什么想法吗?

谢谢

最佳答案

同步或更好的理解让我们首先了解同步这个词的意思。

暂时不要使用字典定义,它会让您感到困惑。相反,让我们做一个过于简单的类比:

When we walk, our legs are synchronized! That is, they coordinate/cooperate together to complete an activity called walking. When left foot touches the ground the right foot leaves it (or is in process of leaving), and when right foot touches its vice-versa.

The key takeaway is, both feet have an implicit coordination stating that other will start (i.e. leave ground) only when first one is grounded.

所以就线程和共享资源而言。同样的类比也适用。所有线程都希望使用一些共享资源(在我们的案例中)。因此,他们需要“协调”资源的使用。

所以当您听到“同步”这个词时,请想到这个词coordinate/cooperate。希望你现在明白了。我故意不解释 SynchronizationContext 因为你说你已经知道了。

更新:第二点,我也应该在这里为您提供一些类比方面的细节:

希望您现在明白同步意味着什么!就 SynchronizationContext 而言,它只是一个允许您与现有 UI 线程的资源(主要是 UI 组件和状态)进行同步(即协调/合作)的类。通常实现的方式是将消息放入 UI 线程的队列中,而不是自己直接修改 UI 状态。

免责声明:不要自己尝试步行类比,我只是在尝试找出我的腿的同步时减掉了 2 卡路里。

关于C# "Synchronization"中的 "SynchronizationContext"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52108250/

相关文章:

winforms - 如果存在 System.Windows.Forms.Form 实例,await Task.Delay(...) 会卡住

c# - 如何获得显示的第二种形式的同步上下文

c# - DataReader (DbCommand) 如何处理 DB 通信?

c# - 关于线程池最大线程的说明

c# - 使用 C# 进行多平台开发

c# - async/await 与 ConfigureAwait 的 continueOnCapturedContext 参数和用于异步延续的 SynchronizationContext

c# - 是否可以控制 .NET Progress<T> 对象使用的 SynchronizationContext?

c# - WPF MVVM 中的动态 TabControl

c# - 异步方法未按正确(预期)顺序执行

c# - 当主线程终止时 SynchronizationContext 会发生什么?