I am lost when trying to understand why this error is being thrown. I wasn't aware I was using a worker thread in my code. Please could somebody help me understand what is happenning...
I have two forms, called for arguments sake 'Main' and 'Explorer'
When a button is clicked in 'Explorer' it calls a public sub in 'Main' called SendReceive() which executes, as the name suggests, a sendreceive in the outlook namespace. It also sets up a handler for the syncend() event, called private sub SyncEnd(). When this event is called it removes the handler, kills the outlook.exe process then calls a sub in the 'Explorer' form to refresh it's view of the folder.
It's this last call that throws the exception. I am left assuming that the synend handler is being run in a seperate thread. If that is so, how do i call back to the 'Explorer' Form?
See code below.
Any help is appreciated. Thanks in advance.
' Main routines
Public Sub SendReceive()
oApp = New Outlook.Application
Dim oNS As Outlook._NameSpace = oApp.GetNamespace("mapi")
Dim oSyncs As Outlook.SyncObjects
Dim oSync As Outlook.SyncObject
oSyncs = oNS.SyncObjects
oSync = oSyncs.Item("All Accounts")
instance = oSync
AddHandler instance.SyncEnd, AddressOf SyncEnd
oSync = Nothing
oSyncs = Nothing
oNS = Nothing
oApp = Nothing
Catch ex As Exception
Private Sub SyncEnd()
RemoveHandler instance.SyncEnd, AddressOf SyncEnd
oApp = Nothing
Dim myProcesses As Process() = Process.GetProcessesByName("outlook")
For Each myProcess As Process In myProcesses
If myProcess.MainWindowTitle = "" Then myProcess.Kill()
If Explorer.Visible = True Then Explorer.RefreshItems()
' Explorer routines
Private Sub SendReceive(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tssendreceive.Click
Public Sub RefreshItems()
Dim session As RDOSession = CreateObject("Redemption.RDOSession")
folderitems = GetMailItemsFromFolder(session.GetFolderFromPath(currentfolder))
UpdateItems(SelectRows(sortcolumn & " " & sortorder))