Kahgus
asked on
System.InvalidOperationException was unhandled
These are my code:
Sub AttemptLogin(ByVal val As String)
Dim MDBConn As OleDbConnection
Dim MDBcmd As OleDbCommand
Dim MDBReader As OleDbDataReader
Dim ip_check As Integer
Dim IP_Address As String
Dim frmConUser As New frmConnectUser
frmConUser.StartPosition = FormStartPosition.CenterPa rent
If val = "REFUSE" Then
frmConUser.lblAlert.Text = "**The user account is logged on**"
client.GetStream.Dispose()
client.Close()
ElseIf val = "WRONG" Then
frmConUser.lblAlert.Text = "**The user account is not exsit or the password is not match!**"
client.GetStream.Dispose()
client.Close()
End If
frmConUser.ShowDialog()
If frmConUser.txtUserLogin.Te xt <> "" Then
MDBConn = New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0;Data Source=Database.mdb;")
MDBConn.Open()
MDBcmd = New OleDbCommand("select * from serverlist order by id", MDBConn)
MDBReader = MDBcmd.ExecuteReader
While MDBReader.Read() And ip_check <> 1
Try
IP_Address = CStr(MDBReader(1))
client = New TcpClient(IP_Address, PORT_NUM)
client.GetStream.BeginRead (readBuffe r, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
ip_check = 1
MDBReader.Close()
MDBConn.Close()
SendData("CONNECT|" & frmConUser.txtUserLogin.Te xt & "|" & frmConUser.txtPassword.Tex t)
'frmConnectUser.Dispose()
'frmConnectUser.Close()
mnuConnect.Text = "Disconnect"
Exit While
Catch ex As Exception
ip_check = 2
End Try
End While
If ip_check = 2 Then
MsgBox("Server is not active. Please start server and try again.", MsgBoxStyle.Exclamation, Me.Text)
Exit Sub
End If
ElseIf mnuConnect.Text = "Disconnect" Then
SendData("DISCONNECT")
MarkAsDisconnected()
client.Close()
mnuConnect.Text = "Connect"
End If
End Sub
I have got error msg for "frmConUser.ShowDialog()" when I run twice times:
System.InvalidOperationExc eption was unhandled
Message="復åà ‚ŽÂŸÃ ¤Ã‚½Âœà ƒÂ¦Ã‚¥Â Ã§Âšà ‚„å …§åà ‚®Â¹Ã ¨Ã‚ˆÂ‡à ƒÂ¥Ã‚¥Â —ç”à ‚¨Ã¨Â ‡Ã‚³Ã¥à ‚°ÂÂà¦Ã‚‡Â‰à ƒÂ¨Ã‚¨Â Ã¥Â®à ‚šÃ¤Â ½Ã‚œÃ¦à ‚¥ÂÂà§Ã‚šÂ„à ƒÂ¥Ã‚… §ÃƒÂ¥Ã‚®à ‚¹Ã¤Â ¸Ã‚ÂÃ¥à ‚ÂΈ£Ã‚€Â‚à ƒÂ¥Ã‚Â ¯ÃƒÂ¨Ã‚Ĉ ‚½Ã¦Â ˜Ã‚¯Ã¥à ‚›Â Ã §Ã‚⼈ ƒÂ¥Ã‚… §ÃƒÂ¥Ã‚®à ‚¹Ã¥Â ·Ã‚²Ã¨à ‚¨ÂÂà¥Ã‚®Âšà ƒÂ¥Ã‚œÂ ¨ÃƒÂ¥Ã‚Ÿà ‚·Ã¨Â ¡Ã‚ŒÃ§à ‚·Â’à¤Ã‚¸ÂŠà ƒÂ¯Ã‚¼Â ŒÃƒÂ¤Ã‚½à ‚†Ã¦Â ²Ã‚’æà ‚œÂ‰Ã ©Ã‚‚„à ƒÂ¥Ã‚ŽÂ Ÿ (復原) ã€Â ‚"
Source="mscorlib"
StackTrace:
於 System.Threading.Synchroni zationCont extSwitche r.Undo()
於 System.Threading.Execution ContextSwi tcher.Undo ()
於 System.Threading.Execution Context.ru nFinallyCo de(Object userData, Boolean exceptionThrown)
於 System.Runtime.CompilerSer vices.Runt imeHelpers .ExecuteBa ckoutCodeH elper(Obje ct backoutCode, Object userData, Boolean exceptionThrown)
於 System.Runtime.CompilerSer vices.Runt imeHelpers .ExecuteCo deWithGuar anteedClea nup(TryCod e code, CleanupCode backoutCode, Object userData)
於 System.Threading.Execution Context.Ru nInternal( ExecutionC ontext executionContext, ContextCallback callback, Object state)
於 System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
於 System.Net.ContextAwareRes ult.Comple te(IntPtr userToken)
於 System.Net.LazyAsyncResult .Protected InvokeCall back(Objec t result, IntPtr userToken)
於 System.Net.Sockets.BaseOve rlappedAsy ncResult.C ompletionP ortCallbac k(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
於 System.Threading._IOComple tionCallba ck.Perform IOCompleti onCallback (UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
Sub AttemptLogin(ByVal val As String)
Dim MDBConn As OleDbConnection
Dim MDBcmd As OleDbCommand
Dim MDBReader As OleDbDataReader
Dim ip_check As Integer
Dim IP_Address As String
Dim frmConUser As New frmConnectUser
frmConUser.StartPosition = FormStartPosition.CenterPa
If val = "REFUSE" Then
frmConUser.lblAlert.Text = "**The user account is logged on**"
client.GetStream.Dispose()
client.Close()
ElseIf val = "WRONG" Then
frmConUser.lblAlert.Text = "**The user account is not exsit or the password is not match!**"
client.GetStream.Dispose()
client.Close()
End If
frmConUser.ShowDialog()
If frmConUser.txtUserLogin.Te
MDBConn = New OleDbConnection("Provider=
MDBConn.Open()
MDBcmd = New OleDbCommand("select * from serverlist order by id", MDBConn)
MDBReader = MDBcmd.ExecuteReader
While MDBReader.Read() And ip_check <> 1
Try
IP_Address = CStr(MDBReader(1))
client = New TcpClient(IP_Address, PORT_NUM)
client.GetStream.BeginRead
ip_check = 1
MDBReader.Close()
MDBConn.Close()
SendData("CONNECT|" & frmConUser.txtUserLogin.Te
'frmConnectUser.Dispose()
'frmConnectUser.Close()
mnuConnect.Text = "Disconnect"
Exit While
Catch ex As Exception
ip_check = 2
End Try
End While
If ip_check = 2 Then
MsgBox("Server is not active. Please start server and try again.", MsgBoxStyle.Exclamation, Me.Text)
Exit Sub
End If
ElseIf mnuConnect.Text = "Disconnect" Then
SendData("DISCONNECT")
MarkAsDisconnected()
client.Close()
mnuConnect.Text = "Connect"
End If
End Sub
I have got error msg for "frmConUser.ShowDialog()" when I run twice times:
System.InvalidOperationExc
Message="復åÃ
Source="mscorlib"
StackTrace:
於 System.Threading.Synchroni
於 System.Threading.Execution
於 System.Threading.Execution
於 System.Runtime.CompilerSer
於 System.Runtime.CompilerSer
於 System.Threading.Execution
於 System.Threading.Execution
於 System.Net.ContextAwareRes
於 System.Net.LazyAsyncResult
於 System.Net.Sockets.BaseOve
於 System.Threading._IOComple
Try catch that exception and format it with CultureInfo.InvariantCultu re, or CultureInfo.GetCulture("en -US"). After that post that exception string (Exception.ToString(cultur e)) here.
If only one thread call to ShowDialog(), it'll run fine.
I guess it's called from different threads; current thread and thread-pool thread.
Thread-pool thread is automatically lunched after you call client.GetStream.BeginRead (), and the DoRead() will run in the new thread.
I hope you did not call AttemptLogin() from the DoRead(). But you can use Show() instead in multi-threads.
I guess it's called from different threads; current thread and thread-pool thread.
Thread-pool thread is automatically lunched after you call client.GetStream.BeginRead
I hope you did not call AttemptLogin() from the DoRead(). But you can use Show() instead in multi-threads.
frmConUser.Show(Me) 'Me should be top-level form
ASKER
Hi gnoon,
But I need collect some value from frmConUser, and then affect frmMain to run some function.
If I use .show(), how to do that?
Or any solution to handle showdialog() in multithread
But I need collect some value from frmConUser, and then affect frmMain to run some function.
If I use .show(), how to do that?
Or any solution to handle showdialog() in multithread
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Tank you for your help