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?

2
  • NotifyIcon doesn't inherit from System.Windows.Forms.Control - does that change the premise of your question? Commented Apr 30, 2015 at 10:37
  • @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. Commented Apr 30, 2015 at 10:47

1 Answer 1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.