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

Re: Some basic questions on using threads. by Elisa

Elisa
Mon Feb 09 09:45:57 CST 2004

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

---------

Daniel Strigard wrote:

> 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

RE: Some basic questions on using threads. by anonymous

anonymous
Tue Feb 10 02:36:05 CST 2004

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


Re: Some basic questions on using threads. by Geoff

Geoff
Tue Feb 10 14:30:53 CST 2004

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" <anonymous@discussions.microsoft.com> wrote in message
news:F2DAF88D-A941-4FC0-8228-D6617A90D62C@microsoft.com...
> 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.
>
> Andy
>
>
>