Some basic questions on using threads.

G

Guest

Hi.

I have 3 questions. For the short version: skip to the end of the post

For those of you who has some time over: Here comes the long version of the questions, with some background.

I’ve done a program that sends a small message on the comport and then receives a large chunk of data. When I’ve received my data I want to let the user start fiddling with it. But at the same time I’d like to start a little code running in the background. This new code should send 4 small messages/second on the comport and receive a value each time, which I’d like to display to the user while he’s fiddling with the other data. This seems to me like a task for multiple threads, right? (Haven’t used ‘em before…)

Since I need other tasks to run at the same time, I guess I can’t just use a timer. I had an idea of having a thread running a separate class with the background program. The thread could run the code and then sleep for 250 ms. Does that seem like the right way to go about? Although that wouldn’t make the 250 ms interval very accurate, since it would depend on the execution time of the code. How would I get the code to start running with exact interval?

Another problem I have is that I need to call procedures in other classes. As far as I can understand this demands that the called sub is public and shared, but then I get problems since that sub often refers to instance members of that class (such as controls) and even properties of other instanced classes. I could perhaps try to make everything shared, but that doesn’t seem like a good way to solve things. Or is it the only way?

This also applies to my “background codeâ€, since I’d like it to call a procedure in another class (the code handling communication with the comport). But can one do that with a separate thread? Do I need to rewrite that code inside the thread?

In short:
1. How to run code at exact intervals (using threads)?
2. Is it possible to somehow use instanced members when calling a sub in another class?
3. Can a thread call procedures outside of its class?

I’d be very thankful for some help on any of my questions.
/D
 
E

Elisa

Hi Daniel,

I'm not an expert in multi-threading, but in general, from inside a
thread you can do everything you can do from outside a thread. You can
call methods in any class, weither these methods are shared or not, you
can access instance data, etc.

But (and what a big but!): as soon as multi-threading comes into the
picture, your main concern is to synchronize these threads.
Synchronizing threads means avoiding race conditions and deadlocks. Race
conditions happen when one thread reads data that another thread is
currently modyfying. Deadlocks happen when thread A is waiting for a
locked resource inside thread B, but thread B isn't releasing the
resource because it's waiting for thread A to release a locked resource
first. Anything that can possible be accessed by two or more threads at
the same time needs to be synchronized, meaning you need to use stuff
like Mutex'es, SyncLock's, Monitor's, ...

Anything that can be accessed includes: GUI Controls, Collections,
variables, objects, handles, databases, com ports, files, ...

Also, make sure your thread behaves nicely amongst it pals, and doesn't
hog all CPU power. Thus, lock things only for as short a time as
possible, call Thread.Sleep(0) often, and check regularly if your thread
needs to stop running.

For your other question, no, you can't run code at an EXACT interval,
and certainly not for small interval's.

Before you start use threads, make sure you read all you can about them
(e.g. on MSDN), how to use threads with GUI controls, how to use threads
with Collections, how to use SyncLock's, how to debug a multi-threaded
application (yes, you can start screaming now...), etc. There is a very
steep learning curve involved, and if you don't understand the full
picture, it simply will wreck havoc on your whole app (and other running
apps, for that matter)!


Regards,

Elisa

---------
 
G

Guest

Thanks for your help and suggestions Elisa and Andy
I guess I have some reading and learning to do then
Better get on with it
/Danie
 
G

Geoff Schwab [MSFT]

Regarding TickCount, take a look at this FAQ item. I suggest using
performance counters...

6.9. How do I use the performance counter functions?
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx#6.9

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
Andy Roxburgh said:
In terms of timing you could use
System.Environment.TickCount
which isn't perfect but has always been satisfactory for my uses.
You could compare that to a previously stored value at the beginnng of
your worker thread code and only execute the code once xx ms have passed.
There are probably more accurate ways to do it though, just a suggestion!
As for accessing say, GUI controls from threads, use ControlInvoke. Do a
search for ControlInvoker on Google and you'll find some example code.
 
R

Richard Kucia

This FAQ article explains how to read a time value that's precise (assuming
the system implements it). How would I use this information to cause a
precise time delay in a program? Are system timers, Windows Forms timers,
Sleep accurate enough to make the performance counters useful, or do they
suffer from the same inaccuracy problems as TickCount? Or do I need to do
this ugly pseudo-code in order to get a precise delay?

finish_time = get_perf_counter_time() + interval_to_wait
do until get_perf_counter_time() >= finish_time
Application.DoEvents
loop

Thanks
Richard Kucia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top