Why does NotifyIcon not set SynchronizationContext?
Asked Answered
V

1

2

Consider this WinForms program:

Module Main
    Dim notifyicon As New System.Windows.Forms.NotifyIcon
    'Dim dummycontrol As New System.Windows.Forms.Control

    Public Sub Main()
        If (System.Threading.SynchronizationContext.Current Is Nothing) Then
            MessageBox.Show("Nothing")
        Else
            MessageBox.Show("Something")
        End If
    End Sub
End Module

NotifyIcon is a WinForm control, and requires a message loop, so why will declaring dummycontrol (or any WinForms control) set a SynchronizationContext, but the NotifyIcon does not?

Verboten answered 30/4, 2015 at 10:25 Comment(2)
NotifyIcon doesn't inherit from System.Windows.Forms.Control - does that change the premise of your question?Quern
@JamesThorpe Thank you; a telling observation for sure, and explains why it doesn't exhibit the same behavior as other controls, but I'm still curious why a context wouldn't be set despite that.Verboten
S
3

This is something you can discover from the Reference Source, the synchronization provider is installed by the WindowsFormsSynchronizationContext.InstallIfNeeded() method. Look at the references to see when it is called:

  • Application.Run()
  • The Control class constructor
  • The helper method that dispatches Begin/Invoke() calls (won't happen).

NotifyIcon is derived from Component, not Control, so never hits one of those 3 bullets. It is a thin wrapper around the Shell_NotifyIcon() winapi function. I suppose you can call it bug that its constructor doesn't call InstallIfNeeded() but that's a bit of a stretch, you always must call Application.Run() to make it functional so you'll always hit the 1st bullet. Just beware initialization order.

Severson answered 30/4, 2015 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.