VB101SamplesBCL2 Using The serial PORt..

Well the time has come to start messing with the serialport...AHHHHH..

Downloaded the VB101SamplesBCL2 and loaded up the Usingtheserialport project.. Answered a lot of question right off the bat, BUT... I seem to have some strangeness going on.

I built the project and moved the exe to a win98 test machine I use. All runs fine on that machine. I run the project in the enviroment on the XP machine and in the

serialPort_DataReceived 'event' I get InvalidOperationException... cross-thread calls

Begin Post::

System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'textBox2' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.set_WindowText(String value)
at System.Windows.Forms.TextBoxBase.set_WindowText(String value)
at System.Windows.Forms.Control.set_Text(String value)
at System.Windows.Forms.TextBoxBase.set_Text(String value)
at System.Windows.Forms.TextBox.set_Text(String value)
at UsingTheSerialPort.Form1.serialPort_DataReceived(Object sender, SerialDataReceivedEventArgs e) in C:\Documents and Settings\Jim\My Documents\MSDN\VB101SamplesBCL2\UsingTheSerialPort\Form1.vb:line 40
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)

ENd Post::

Nice sample? Works compiled on a Win98 OK.. Well At least it doesn't crash..

Will run the exe on the xp machine and look for solutions for cross thread calls etc..

Just wondering if anyone else had seen this.. I did try to do a search here but found nothing

st..

[2522 byte] By [stubs] at [2007-12-22]
# 1
The

DataReceived event runs on a different thread from your main program's

GUI thread. That means you can't directly access your form's

controls. You'll have to use the form's Invoke method.

nobugz at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Did some searching and found some referances to the invoke method.. seems to be the way to go. THanks for the confermation on it...

The real question then I guess is why don't they use this method in the samples?

And the other question is..... WHY does the compiled "EXE" work? BUT NOT the code when run in the VB enviroment?

st

stubs at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3
Not

sure, I didn't look at the sample. In the big scheme of things, I

guess that a sample is made available to satisfy everyone's "look at

that!" feeling, then to tinker with and get into trouble and learn more

about how it all works. It seems to have worked, you already have

a lot more insight than the average first user when you grok Invoke...

nobugz at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

Well I'm not exactly 'New' at VB just the .net stuff... started out with GWbasic a couple of 'weeks' , seems like, ago.. and have moved along in jumps and spurts ever sence..

Do mostly motion control and industrial U.I. programming..

Thanks again....

st

stubs at 2007-8-30 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...