Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

System.InvalidOperationException was unhandled

Posted on 2007-11-21
5
Medium Priority
?
2,490 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

972 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