From: mind_the_gap on
Hello there,

currently I am working on a c# project with the .Net Compact Framwork.
I have to use two threads where one of them modifies a member of the
class in regular timed intervals. The "normal" (e.g. the first or
start or main thread :P) thread needs to read this data to display it.
So can this bring up some errors or problems (like asyncronous reading/
writing) and do I need a mutex for writing and reading the data from
different threads?
Are there any examples for this? I am relatively new to threads.

Thanks in advance
Tom
From: Simon Hart [MVP] on
You're better off using an event in the worker thread that modifies the data
so when it modifies the data the client can then know about the new data so
that it can display it. this would be much cleaner.

Alternatively, simply use the lock keyword only in the worker thread if
that's the only thread that sets the variable, ie something like the
following:

string mySharedVar = "blah";
lock(mysharedVar)
{
mySharedVar = "Hello World!";
}

But as I say, i'd use an event/delegate model.
--
Simon Hart
Visual Developer - Device Application Development MVP
http://simonrhart.blogspot.com


"mind_the_gap" wrote:

> Hello there,
>
> currently I am working on a c# project with the .Net Compact Framwork.
> I have to use two threads where one of them modifies a member of the
> class in regular timed intervals. The "normal" (e.g. the first or
> start or main thread :P) thread needs to read this data to display it.
> So can this bring up some errors or problems (like asyncronous reading/
> writing) and do I need a mutex for writing and reading the data from
> different threads?
> Are there any examples for this? I am relatively new to threads.
>
> Thanks in advance
> Tom
>
From: Freesc on
On Jan 7, 2:03 am, mind_the_gap <2voo...(a)gmx.de> wrote:
> Hello there,
>
> currently I am working on a c# project with the .Net Compact Framwork.
> I have to use two threads where one of them modifies a member of the
> class in regular timed intervals. The "normal" (e.g. the first or
> start or main thread :P) thread needs to read this data to display it.
> So can this bring up some errors or problems (like asyncronous reading/
> writing) and do I need a mutex for writing and reading the data from
> different threads?
> Are there any examples for this? I am relatively new to threads.
>
> Thanks in advance
> Tom

Control.Invoke() method will be a good choice ~
From: mind_the_gap on
On Jan 7, 7:53 am, Freesc <hjd.cl...(a)gmail.com> wrote:
> On Jan 7, 2:03 am, mind_the_gap <2voo...(a)gmx.de> wrote:
>
> > Hello there,
>
> > currently I am working on a c# project with the .Net Compact Framwork.
> > I have to use two threads where one of them modifies a member of the
> > class in regular timed intervals. The "normal" (e.g. the first or
> > start or main thread :P) thread needs to read this data to display it.
> > So can this bring up some errors or problems (like asyncronous reading/
> > writing) and do I need a mutex for writing and reading the data from
> > different threads?
> > Are there any examples for this? I am relatively new to threads.
>
> > Thanks in advance
> > Tom
>
>  Control.Invoke() method will be a good choice ~

Thanks for the replys.
I tryed with events - the problem with this seems that the event code
is only run in the thread where it was fired - so in my case the
thread that changes the variable.
Or am I missing something. The solution with lock() seems to works - I
think I do this. But maybe I am missing something with those events.

Control.Invoke() can not be used as the "user" of the variable - the
class member using the variable and thus the class - does not extend
Control. This means I can not use this. It seems that lock() is the
keyword for non-control classes?

Thanks
From: " ctacke/>" on
lock and Control.Invoke are totally unrelated, so they have nothing to do
with one another. The lock keyword is simply a construct that gives a try
with a Monitor.Enter and a finally block with a Monitor.Exit. You can
Invoke from inside it or without it and from anay thread.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com



"mind_the_gap" <2voodoo(a)gmx.de> wrote in message
news:2b785900-7001-4ad4-b76d-75ca384c368c(a)m77g2000hsc.googlegroups.com...
On Jan 7, 7:53 am, Freesc <hjd.cl...(a)gmail.com> wrote:
> On Jan 7, 2:03 am, mind_the_gap <2voo...(a)gmx.de> wrote:
>
> > Hello there,
>
> > currently I am working on a c# project with the .Net Compact Framwork.
> > I have to use two threads where one of them modifies a member of the
> > class in regular timed intervals. The "normal" (e.g. the first or
> > start or main thread :P) thread needs to read this data to display it.
> > So can this bring up some errors or problems (like asyncronous reading/
> > writing) and do I need a mutex for writing and reading the data from
> > different threads?
> > Are there any examples for this? I am relatively new to threads.
>
> > Thanks in advance
> > Tom
>
> Control.Invoke() method will be a good choice ~

Thanks for the replys.
I tryed with events - the problem with this seems that the event code
is only run in the thread where it was fired - so in my case the
thread that changes the variable.
Or am I missing something. The solution with lock() seems to works - I
think I do this. But maybe I am missing something with those events.

Control.Invoke() can not be used as the "user" of the variable - the
class member using the variable and thus the class - does not extend
Control. This means I can not use this. It seems that lock() is the
keyword for non-control classes?

Thanks