i designed an async socket client, after connection i evoke this OnConnect routine.
The goal is to set a status text in the Main window to "connected" and then show a login dialog to the user
Friend Sub OnConnect(ByVal ar As IAsyncResult)
Try
oSocket.EndConnect(ar)
MainDialog.SetStatus("Connected") <-- this line is giving the error
'We are connected so start listening for messages
byteData = New Byte(1023) {}
'Start listening to the data asynchronously
oSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
'// show login dialog
loginDlg = New LoginDialog
loginDlg.ShowDialog()
Catch ex As Exception
ShowMessage(String.Format(My.Resources.error_failed_reason, "connect", "server", ex.Message), MessageBoxIcon.Information)
End Try
End Sub
but i get an exception
An error occurred creating the form. See Exception.InnerException for details. The error is: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
I am not using any thread , just async socket
what could be causing this error? is the OnConnect invoked in another thread?
EDIT
I just made my application single thread by adding this
Public Class Program
<STAThread()> _
Shared Sub Main()
Dim frm As New MainDialog
Application.Run(frm)
End Sub
End Class
EDIT 2
I replaced this line
MainDialog.SetStatus("Connected")
With this line
If MainDialog.InvokeRequired Then <-- This line gives the same error as above
MainDialog.Invoke(New LoginDelegate(AddressOf ShowLogin), "Connected")
End If
I created this delegate in the module above
Private Delegate Sub LoginDelegate(ByVal Item As Object)