Variables in a multi thread environment.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, Happy New Year!

Im writing a primary singlethreaded program. However I would like to insert
a timer to do a check by reading some values in a file if accessable. The
timer thread should set a boolean to either true or false depending on wether
or not the user has responded to a messagebox created by the thread. This
boolean should be accessible by subsequent timer threads in order to avoid
the screen filling up with messageboxes. How can I make suck a boolean
visible to each timer thread - is there a way to protect it from being
adressed by two threads at the same time. I'm a former C++ programmer and did
a little multi threaded programming and know only little about the multi t
concepts. Thanks.

Jesper, Denmark,
 
you want to use a threadsafe singleton to hold the values, and then, when a
thread needs the value, the singleton object should lock itself to prevent
access by another thread.

Here's one of Jon's pages on singletons:
http://www.yoda.arachsys.com/csharp/singleton.html

Here's a link to info on object locking. The third page goes into locking,
but the whole article is worth reading
http://www.yoda.arachsys.com/csharp/threads/

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Jesper said:
Im writing a primary singlethreaded program. However I would like to insert
a timer to do a check by reading some values in a file if accessable. The
timer thread should set a boolean to either true or false depending on wether
or not the user has responded to a messagebox created by the thread. This
boolean should be accessible by subsequent timer threads in order to avoid
the screen filling up with messageboxes. How can I make suck a boolean
visible to each timer thread - is there a way to protect it from being
adressed by two threads at the same time. I'm a former C++ programmer and did
a little multi threaded programming and know only little about the multi t
concepts. Thanks.

See http://www.pobox.com/~skeet/csharp/threads
 
Back
Top