开发者

ISynchronizeInvoke in .NET

Explain the use of ISynchronizeIn开发者_Python百科voke in cross thread invocation in .NET?


It's just an interface which contains appropriate methods to:

  • Check whether special handling is required (InvokeRequired)
  • Invoke a delegate safely from a different thread, blocking until the action is completed (Invoke)
  • Invoke a delegate safely from a different thread, returning immediately without blocking (BeginInvoke which can be coupled with EndInvoke to retrieve the result later)

The most commonly known implementation is Control in Windows Forms.

Basically the problem is that it's not safe to access UI elements within arbitrary threads. These mechanisms allow calls to be marshalled between threads relatively easily. By only tying yourself to ISynchronizeInvoke instead of Control, you can avoid business classes from being tightly coupled to the UI.

EDIT: Stephen's comment from below is worth repeating here:

It's important to note that ISynchronizeInvoke is outdated. The SynchronizationContext abstraction is more universal. Control is not just the most commonly known implementation; it's the only Microsoft one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜