473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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)
  5.  
  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
  11.  
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
  4.  
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)
  4.  
  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)
  8.  
  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
  14.  
  15.     End Sub 'SendCallback
  16.  
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
1 2623
Tenowg
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

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

Similar topics

4
by: Hari Koduru | last post by:
Hi All, I am getting the following exception in an ASP.NET application. Exception Details: System.InvalidOperationException: Application is restarting. I have been to the following Support...
6
by: Tony | last post by:
Dear All, When I run an example program on the http://www.dotnetjunkies.com/quickstart/util/srcview.aspx?path=/quickstart/aspplus/samples/webforms/data/datagrid1.src&file=VB\datagrid1.aspx&font=3 ...
1
by: Trygve Lorentzen | last post by:
Hi, my webservice is running on Win2000 SP4, IIS 5.0 fully patched, connecting to a MySQL database and mainly returning Typed DataSet's from webmethods. After running for a while, generally a...
0
by: nirupama | last post by:
Sir, I am new to the Nunit,nunitasp i am gettin an error as "System.InvalidOperationException:the specified node cannot be inserted as the valid child of this node, because the specified node...
0
by: banduraj | last post by:
I get an System.InvalidOperationException when trying to Serialize a struct that has a type refrenced from another library. The exception is: System.InvalidOperationException was unhandled...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
3
by: Joseph Geretz | last post by:
System.InvalidOperationException: WebServiceBindingAttribute is required on proxy classes. My environment: Visual Studio 2005, targeting FX 2.0; I've developed a Web Service which uses DIME to...
0
by: John H | last post by:
Hi, Strange error with serialisaation on .net 1.1 Sp1 when a the assembly containing the type is in the Gac as well as on e.g c:\customdlls. ObjValue in below code is populated with and array of...
1
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread. Logic is: 1) i will first create a thread to display a splash screen until a big process is completed 2)then i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.