Question

In my Outlook 2013 VSTO AddIn, I have this code:

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    Task.Run(Sub() DoStuff())
End Sub

Private Sub DoStuff()
    ' Do some long task stuff
    Call StuffDone() 
End Sub

Private Sub StuffDone()
    If ????.InvokeRequired Then
        ????.Invoke(New MethodInvoker(AddressOf StuffDone))
        Exit Sub
    End If

    Dim f As New Form1
    f.Show()
End Sub

But I don't know how to call InvokeRequired since there's no mainform??

Thanks

Was it helpful?

Solution

Why do you feel you need to call InvokeRequired?

What I'm doing in my case is to include a Windows Form in the VSTO project. I pass the Outlook Application reference from the Add-in to the Windows Form and then my Windows form may spawn one or more threads that may also receive and access the Outlook Application (after locking on the reference). The Windows Form methods that the threads call to update the GUI have the InvokeRequired check that you mentioned. I can't guarantee this is the Microsoft recommended approach but I can say its been working for me error-free for over a year in my case.

If you prefer to do it your way I think it would be: if (f.InvokeRequired) ...

but I'm not sure why that's necessary

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top