Solved

System.InvalidOperationException was unhandled

Posted on 2007-11-21
5
2,469 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now