By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,407 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

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

P: 9
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.InvalidOperationException 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.

Expand|Select|Wrap|Line Numbers
  1. Public Class AsynchronousSocketListener
  2.     ' Thread signal.
  3.     Public Shared allDone As New ManualResetEvent(False)
  4.     Delegate Sub UpdateTextBox(ByVal message As String)
  6.     Private Shared Sub Handler1(ByVal strInMessage As String)
  7.         Dim f As Form1 = My.Application.OpenForms("Form1")
  8.         f.Invoke(New UpdateTextBox( _
  9.            AddressOf f.UpdateMessage), New Object() {strInMessage})
  10.     End Sub
and the code from the Form1.vb

Expand|Select|Wrap|Line Numbers
  1.    Public Sub UpdateMessage(ByVal strInMessage As String)
  2.         Me.TextBox1.Text = strInMessage & vbCrLf & Me.TextBox1.Text
  3.     End Sub
at then end of my async class:

Expand|Select|Wrap|Line Numbers
  1.     Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
  2.         ' Retrieve the socket from the state object.
  3.         Dim handler As Socket = CType(ar.AsyncState, Socket)
  5.         ' Complete sending the data to the remote device.
  6.         Dim bytesSent As Integer = handler.EndSend(ar)
  7.        Handler1("Sent " & bytesSent & " bytes to client." & vbCrLf & Form1.TextBox1.Text)
  9.         handler.Shutdown(SocketShutdown.Both)
  10.         handler.Close()
  11.         ' Signal the main thread to continue.
  12.         allDone.Set()
  13.         'Form1.TextBox1.Text = "Sent " & bytesSent & " bytes to client." & vbCrLf & Form1.TextBox1.Text
  15.     End Sub 'SendCallback
when I comment out

Expand|Select|Wrap|Line Numbers
  1.         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.
Sep 7 '07 #1
Share this Question
Share on Google+
1 Reply

P: 9
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.
Sep 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.