getting a System.InvalidOperationException was unhandled (no specific location given)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tenowg
    New Member
    • Sep 2007
    • 9

    getting a System.InvalidOperationException was unhandled (no specific location given)

    Hey everyone... Firstly let me tell you this is a project that is probably alittle over my head... I have played with programming most of my life, but I have only been doing VB 2005 for about 2 weeks. I know the concepts of most issues, but multi-threading seems to be giving me a headache... anyways here is what is happening...

    I have use the MSDN code sample to create a Async TCP server (using sockets, and beginaccept, etc, not Tcplistener or TcpClient), and converted it some to work in a windows form. I got that working, sending all information to a Msgbox as it is transfered from the Console application for the client, no bugs were in this part of the code as far as I know. But on attempting to change Textbox1 in Form1, I realized that I couldn't (it was in the original thread, whereas the message was in the async tcp thread). I have spent a few days trying to learn to use the invoke statement. Seems it is working, as the textbox does get changed to reflect my stream that I recieve from the client. BUT and there is always a BUT I guess, I now get this error at the last End Sub of my async server code.

    Getting System.InvalidO perationExcepti on was unhandled

    The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone ).

    this is the code I added to the server code to invoke the textbox1 in form1.

    Code:
    Public Class AsynchronousSocketListener
        ' Thread signal.
        Public Shared allDone As New ManualResetEvent(False)
        Delegate Sub UpdateTextBox(ByVal message As String)
    
        Private Shared Sub Handler1(ByVal strInMessage As String)
            Dim f As Form1 = My.Application.OpenForms("Form1")
            f.Invoke(New UpdateTextBox( _
               AddressOf f.UpdateMessage), New Object() {strInMessage})
        End Sub
    and the code from the Form1.vb

    Code:
       Public Sub UpdateMessage(ByVal strInMessage As String)
            Me.TextBox1.Text = strInMessage & vbCrLf & Me.TextBox1.Text
        End Sub
    at then end of my async class:

    Code:
        Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
            ' Retrieve the socket from the state object.
            Dim handler As Socket = CType(ar.AsyncState, Socket)
    
            ' Complete sending the data to the remote device.
            Dim bytesSent As Integer = handler.EndSend(ar)
           Handler1("Sent " & bytesSent & " bytes to client." & vbCrLf & Form1.TextBox1.Text)
    
            handler.Shutdown(SocketShutdown.Both)
            handler.Close()
            ' Signal the main thread to continue.
            allDone.Set()
            'Form1.TextBox1.Text = "Sent " & bytesSent & " bytes to client." & vbCrLf & Form1.TextBox1.Text
    
        End Sub 'SendCallback
    when I comment out

    Code:
            Handler1("Sent " & bytesSent & " bytes to client." & vbCrLf & Form1.TextBox1.Text)
    I will no longer get the error, but the textbox (of course) does not update. And as I mentioned, the textbox does get updated right before I see the error message pup up, with a break in my code the error message shows at the End Sub 'SendCallback

    To some this code may look femiliar as I used an example that was posted on these forms.

    Thanks in advance, and if you need more information feel free to ask, this is just a pet project for me, but something I would really like to fix, my whole project seems to depend on it.
  • Tenowg
    New Member
    • Sep 2007
    • 9

    #2
    Sorry for the friendly bump, but I am now at the point where I need to figure this out, or I can't go any farther in my project...

    thanks, and sorry again.

    Comment

    Working...