Walkthrough: Supporting COM Interop by Displaying Windows Forms on a Shared Thread

You can resolve COM interoperability problems by displaying your form on a .NET Framework message loop, which is created by using the Application.Run method.

To make a Windows Form work correctly from a COM client application, you must run it on a Windows Forms message loop. To do this, use one of the following approaches:

The following procedure demonstrates how to display a Windows Form on a new thread with a shared message loop.

To copy the code in this topic as a single listing, see How to: Support COM Interop by Displaying Windows Forms on a Shared Thread.

This approach is similar to the one shown in Walkthrough: Supporting COM Interop by Displaying Each Windows Form on Its Own Thread. However, instead of displaying each form on its own thread by using its own message loop, this approach creates a shared message loop that runs on only one new thread in the .NET Framework component.

This approach more accurately represents the behavior of a standard Windows Forms application. This approach also makes it easier to share resources between multiple forms, because all the forms run on the same thread. The solution in Walkthrough: Supporting COM Interop by Displaying Each Windows Form on Its Own Thread creates a new thread for each form. That solution requires additional thread synchronization code to share resources between different forms.

Because this approach is more similar to the behavior of a Windows Forms application, you will see that .NET Framework Windows Forms that the client application opens will close when the .NET Framework message loop stops. This behavior occurs when the user closes the form that is designated as the main form for the ApplicationContext. The ApplicationContext is used to start the message loop.

In the following code examples, the main form of the ApplicationContext is set to the first form that the client application opens. Therefore, when the user closes that form instance, the .NET Framework message loop exits, and all other Windows Forms will close.

To create a shared message loop on a new thread for all forms to use

  1. Create a new Class Library project and name it COMWinform.

  2. Delete the default Class1.vb file.

  3. On the Project menu, click Add Class.

  4. Select the COM Class template.

  5. In the Name box, type COMForm.vb, and then click Add.

  6. Paste the following code statements at the top of the COMForm file, before the class definition.

    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
  7. In the COMForm class definition, paste the following code under the constructor definition.

    Private WithEvents frmManager As FormManager
    Public Sub ShowForm1()
        ' Call the StartForm method by using a new instance
        ' of the Form1 class.
        StartForm(New Form1)
    End Sub
    Private Sub StartForm(ByVal frm As Form)
        ' This procedure is used to show all forms
        ' that the client application requests. When the first form
        ' is displayed, this code will create a new message
        ' loop that runs on a new thread. The new form will
        ' be treated as the main form.
        ' Later forms will be shown on the same message loop.
        If IsNothing(frmManager) Then
            frmManager = New FormManager(frm)
        End If
    End Sub
    Private Sub frmManager_MessageLoopExit() Handles frmManager.MessageLoopExit
        'Release the reference to the frmManager object.
        frmManager = Nothing
    End Sub
  8. On the Project menu, click Add Class and select the Class template.

  9. In the Name box, type FormManager.vb, and then click Add.

  10. Replace the contents of the FormManager file with the following code.

    Imports System.Runtime.InteropServices
    Imports System.Threading
    Imports System.Windows.Forms
    <ComVisible(False)> _
    Friend Class FormManager
        ' This class is used so that you can generically pass any
        ' form that you want to the delegate.
        Private WithEvents appContext As ApplicationContext
        Private Delegate Sub FormShowDelegate(ByVal form As Form)
        Event MessageLoopExit()
        Public Sub New(ByVal MainForm As Form)
            Dim t As Thread
            If IsNothing(appContext) Then
                appContext = New ApplicationContext(MainForm)
                t = New Thread(AddressOf StartMessageLoop)
                t.IsBackground = True
            End If
        End Sub
        Private Sub StartMessageLoop()
            ' Call the Application.Run method to run the form on its own message loop.
        End Sub
        Public Sub ShowForm(ByVal form As Form)
            Dim formShow As FormShowDelegate
            ' Start the main form first. Otherwise, focus will stay on the 
            ' calling form.
            ' Create a new instance of the FormShowDelegate method, and
            ' then invoke the delegate off the MainForm object.
            formShow = New FormShowDelegate(AddressOf ShowFormOnMainForm_MessageLoop)
            appContext.MainForm.Invoke(formShow, New Object() {form})
        End Sub
        Private Sub ShowFormOnMainForm_MessageLoop(ByVal form As Form)
        End Sub
        Private Sub ac_ThreadExit(ByVal sender As Object, ByVal e As System.EventArgs) Handles appContext.ThreadExit
            appContext.MainForm = Nothing
            appContext = Nothing
            RaiseEvent MessageLoopExit()
        End Sub
    End Class
  11. On the Project menu, click Add Windows Form, and then click Add.

  12. Add some TextBox controls and a Button control to the form.

  13. Double-click Button1 to add a Click event handler.

  14. Add the following code to the Click event handler.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("Clicked button")
    End Sub
  15. Build the solution.

    This step also registers the project for COM interop on this computer.

To create an executable file that demonstrates COM interop

  1. Start Microsoft Visual Basic 6.0.

  2. Create a new standard EXE project.

  3. On the Project menu, click References.

  4. Add a reference to the COMWinform type library that was generated when you built the Visual Basic 2005 solution.


    If you do not see it in the list, click Browse to locate the type library (.tlb) file manually.

  5. Add a button to the form.

  6. On the View menu, click Code, and then add the following code to the form module.

Option Explicit

Private Sub Command1_Click()
    Dim frm As COMWinform.COMForm
    Set frm = New COMWinform.COMForm
End Sub
  1. On the File menu, click Make EXE to compile the project.

  2. Run the compiled Visual Basic 6.0 executable file.

  3. Click the button to display the Windows Form from the class library that you created earlier.

  4. Set the focus on one of the TextBox controls on the Windows Form, and then press the TAB key to move between the controls.

    Notice that the TAB key moves focus from control to control. Also, notice that the button's Click event is raised when you press the ENTER key.