Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi All,
I have a problem caused I think by the Serial Comms thread and UI thread being out. I am reading back a value, if I put in a MsgBox it works if I have a timer Enabled to check the contents of the incoming text box (its a Rich one!)
VB
Private Sub TmrNoDataAtPort_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TmrNoDataAtPort.Tick


       While (rtbIncoming.Text = "")

       End While
       TmrNoDataAtPort.Enabled = False
       'Thread.Sleep(200)

   End Sub
That does not work however if I put into the timer
VB
txtReadFreqA.Text = rtbIncoming.Text

The numbers appear where they should be however that puts the burden on the tool box timer which I have been caught out by in the past (it works fine now but when the load of the certain PC(s) that this is for. I think I need a more certain way of doing it before I get too far with this...
Glenn
Posted
Comments
TnTinMn 12-Feb-14 13:24pm    
Just throwing this out there as a possible SerialPort example source.
http://www.hardandsoftware.net/Downloads.htm

1 solution

It's a bad idea to use timers for communication. You could use stream much more reliably and easier. It even less understandable that you are already using a thread. You did not explain what kind of timer do you use and why.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only). The same goes for the timer handler (don't use the timer), unless this is the timer System.Windows.Forms.Timer. But this type of timer is too bad for most purposes except some we don't need to discuss here. Don't use any timers at all, anyway.

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
glennPattonWork3 12-Feb-14 11:58am    
I fully agree with you about timers and serial comms, I was using the tool box timer, System.Timer.Windows.Form the problem is the data is appearing in said rich textbox but I think the UI thread is running too fast for the update to be seen if I put a time wasting element it works fine.
Sergey Alexandrovich Kryukov 12-Feb-14 13:58pm    
"Thread is running too fast" simply indicate that you are "using" race condition in your code, which is another big no-no. You need to use real thread synchronization, but use carefully, not the way to defeat the parallelism.
First, let's focus on your main problem: UI invocation. You need to use it, no matter what.
Then, read on race conditions: http://en.wikipedia.org/wiki/Race_condition.
And then, learn thread synchronization primitives.

At this point, I cannot help more, but if you can discuss the goals of your project, I could possibly help with proper code design. It looks like this is the root of the problem, and you hardly will be able to successfully complete your project with your design. I am not 100% sure at this time though as I don't know all the detail...

—SA
glennPattonWork3 13-Feb-14 4:17am    
Thanks
Sergey Alexandrovich Kryukov 13-Feb-14 8:22am    
You are welcome.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900