Solved

System.InvalidOperationException was unhandled

Posted on 2007-11-21
5
2,472 Views
Last Modified: 2010-05-18
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.CenterParent
            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.Text <> "" 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(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
                        ip_check = 1
                        MDBReader.Close()
                        MDBConn.Close()
                        SendData("CONNECT|" & frmConUser.txtUserLogin.Text & "|" & frmConUser.txtPassword.Text)
                        '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.InvalidOperationException was unhandled
  Message="復原作業的內容與套用至對應設定作業的內容不同。可能是因為內容已設定在執行緒上,但沒有還原 (復原)。"
  Source="mscorlib"
  StackTrace:
       ÃƒÂ¦Ã‚–¼ System.Threading.SynchronizationContextSwitcher.Undo()
       ÃƒÂ¦Ã‚–¼ System.Threading.ExecutionContextSwitcher.Undo()
       ÃƒÂ¦Ã‚–¼ System.Threading.ExecutionContext.runFinallyCode(Object userData, Boolean exceptionThrown)
       ÃƒÂ¦Ã‚–¼ System.Runtime.CompilerServices.RuntimeHelpers.ExecuteBackoutCodeHelper(Object backoutCode, Object userData, Boolean exceptionThrown)
       ÃƒÂ¦Ã‚–¼ System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       ÃƒÂ¦Ã‚–¼ System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       ÃƒÂ¦Ã‚–¼ System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       ÃƒÂ¦Ã‚–¼ System.Net.ContextAwareResult.Complete(IntPtr userToken)
       ÃƒÂ¦Ã‚–¼ System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
       ÃƒÂ¦Ã‚–¼ System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
       ÃƒÂ¦Ã‚–¼ System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
0
Comment
Question by:Kahgus
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:wizrr
ID: 20326990
Try catch that exception and format it with CultureInfo.InvariantCulture, or CultureInfo.GetCulture("en-US"). After that post that exception string (Exception.ToString(culture)) here.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20327231
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.
frmConUser.Show(Me) 'Me should be top-level form

Open in new window

0
 

Author Comment

by:Kahgus
ID: 20332238
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
0
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20332671
I think, you have to call ShowDialog() only in UI-thread. The code below, I add code at top of AttemptLogin to check if current thread is UI thread.
'Declare a delegate for AttemptLogin
Delegate Sub AttemptLoginDelegate(ByVal val As String)
 
Sub AttemptLogin(ByVal val As String)
 
  If InvokeRequired Then
    Invoke(New AttemptLoginDelegate(AddressOf AttemptLogin), New Object[]{val})
    Return
  End If
 
  ...
End Sub

Open in new window

0
 

Author Closing Comment

by:Kahgus
ID: 31410331
Tank you for your help
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question