SignalR, Outlook add-in and multithread exception

2019-08-10 12:05发布

问题:

In Outlook I have this code...

_connection = New HubConnection(Me._url)
_connection.Credentials = CredentialCache.DefaultCredentials
_hub = _connection.CreateHubProxy(Me._hubName)
_hub.On(Of String)("NewMessage", Function(message)
                                     Dim f As New TestForm
                                     f.Show()
                                     Return ""
                                 End Function)

_connection.Start()

But showing my form "TestForm" crashes since its in the main thread and SignalR is on another thread.

Any idea how I can make it work?

回答1:

Your best attempt would be to use a dedicated thread to do your SignalR jobs and get back on the main STA thread for accessing Outlook Object Model or displaying forms/WPF components.

This how you can "continue" your work on the main thread: Hooked events Outlook VSTO continuing job on main Thread