Re: Timers and Serial
From: Chris Tacke, eMVP (ctacke_at_spamfree-opennetcf.org)
Date: 06/15/04
- Next message: Eric: "problems with BSSID search in different PPC 2003 devices"
- Previous message: David Gonzales [MS]: "Re: Autorun flash folder name for PXA263 processors??"
- In reply to: Jonathan: "Re: Timers and Serial"
- Next in thread: Dick Grier: "Re: Timers and Serial"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 15 Jun 2004 17:04:48 -0400
I'd have to go back and look at the actual architecture of your app and see
just how Dick is doing his stuff, but my assumption is that his library
receives data on a worker thread and puts it into a buffer, at which point
is fires an event to tell you that it has been received. That event is very
likely occurring in the same context as the worker thread collecting the
data (unless he is marshalling it back to the calling thread).
If your event handler takes a while to do its work, then you could easily be
interrupting what it's doing and changing the thread priority in the handler
may well have an adverse effect on his library performance. As a general
rule, never change the priority of a thread you did not create explicitly.
In this case, your handler should probably be doing nothing more than
pulling data off his queue and putting it into an application buffer for use
and then optionally signally that data has arrived. You could (and I'd
venture to say should) have another worker thread that does nothing but
looks at the buffer (or waits for the aforementioned signal) and then does
something with it, whether it's updating the UI or whatever. The reason I
say it should be another thread is you don't want it impacting UI
performance.
-- Chris Tacke, eMVP Co-Founder and Advisory Board Member www.OpenNETCF.org --- --- Principal Partner OpenNETCF Consulting www.OpenNETCF.com "Jonathan" <Jonathan@discussions.microsoft.com> wrote in message news:91DBBF62-762B-4C31-BF1B-454E21FE82CB@microsoft.com... > I tried using two timers but it seems that the timers are having the same threading problem. My app shows how much data was processed as it is processing it and it just seems to hang. > > Also, I disable one timer at a point in my program but it seems that the reamining timer is being interrupted by itself. Its tick event is called before it can finish the last one. > > Can the thread priority be adjusted? I was looking around and found this code: > System.Threading.Thread.CurrentThread.Priority = Threading.ThreadPriority.Lowest > > Is this property read-only or can the priority be changed? I only found how to change the priority of the current thread so I put that code in the timer's tick event. Can the thread's priority be changed in the tick event? It kind of seesm that the priority needs to be set at form load but i guess it could be set at any time? > > Ideally, I would like my app to only have a single thread and never leave a sub or function until the end is reached. So I was thinking of maybe changing the priority of all threads to low. > > "Chris Tacke, eMVP" wrote: > > > Form Timers run in the same thread context as the UI and will interrupt > > anything in the STA. A threading Timer will run in a separate thread > > context. Changing your timer may fix the problem. > > > > -- > > Chris Tacke, eMVP > > Co-Founder and Advisory Board Member > > www.OpenNETCF.org > > --- > > --- > > Principal Partner > > OpenNETCF Consulting > > www.OpenNETCF.com > > > > > > > > "Jonathan" <Jonathan@discussions.microsoft.com> wrote in message > > news:98E18725-64F6-4F49-AAD4-04647437CBB9@microsoft.com... > > > My process data routine doesn't have an UI in it. The Ui in the On_Comm > > event uses Me.Invoke NewEventhandler to avoid the threading problem. There > > are over a thousand lines in the Porcess Data function which is why I didn't > > post them all. It contains a lot of processing that takes up a lot of cpu. > > > > > > If I understand this correctly, when the On-Comm event is being processed > > and the Tick event of the timer goes off, the program leaves the On_Comm > > event to go to Tick event? And the On-Comm event is never resumed? > > > > > > I think I will try using timers even though I like the On-Comm event more > > also. I am hoping that the tick event on one timer won't leave to get to the > > tick event of another? Do timers have the same threading problem with GUI? > > > > > > Thanks again for the excellent help. > > > > > > "Dick Grier" wrote: > > > > > > > Hi, > > > > > > > > Well, the problem probably lies here: > > > > > > > > success = ProcessData(Data) > > > > > > > > You don't show your ProcessData routine. However, if it even touches a > > UI > > > > element, things will not work well. You need to Invoke a delegate that > > then > > > > calls the ProcessData routine. > > > > > > > > If you take a look at the OnComm code example that is in the download, > > you > > > > will see what I mean. This invocation marshals data from the worker > > thread > > > > (a free AKA MTA thread) to the form thread (STAThread). The Timer event > > is > > > > STAThread(ed) so if it hits while the OnComm event is being processed, > > > > things will go "badly." Invoke is the only out if you use OnComm. > > > > > > > > However... You could disable OnComm (EnableOnComm = False), and use a > > > > separate Timer to poll the SerialPort object. Since this is STAThread, > > the > > > > two timers and everything else, will be happy, happy. > > > > > > > > Personally, I prefer OnComm, but the choice is your own. > > > > > > > > Dick > > > > > > > > Dick > > > > > > > > -- > > > > Richard Grier (Microsoft Visual Basic MVP) > > > > > > > > See www.hardandsoftware.net for contact information. > > > > > > > > Author of Visual Basic Programmer's Guide to Serial Communications, 3rd > > > > Edition ISBN 1-890422-27-4 (391 pages) published February 2002. > > > > > > > > > > > > > > > > > >
- Next message: Eric: "problems with BSSID search in different PPC 2003 devices"
- Previous message: David Gonzales [MS]: "Re: Autorun flash folder name for PXA263 processors??"
- In reply to: Jonathan: "Re: Timers and Serial"
- Next in thread: Dick Grier: "Re: Timers and Serial"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|