using a timer, do i need to threadsafe lock methods called ?

P

Peted

i have an mdi application with two child forms

Childform A and childform B



in a nutshell

Childform B has a timer with a routine that polls a ip socket for
information every 30sec.

It does this by calling a method lin the Childform A class.


This same method in childform A can also be called by button clicks
and user iteraction with childform A. In addition user on childform A
can not only manually (via button clicks) get data from this same
socket, but also using another method send data to this same socket


My question is

With the timer running every 30sec on childform B, do i need to
somehow lock the methods on childform A, incase the user does a button
press at the same time as the 30sec poll occurs.

if so, whats the best aproach


any advice appreciated. thanks
 
P

Peter Duniho

[...]
My question is

With the timer running every 30sec on childform B, do i need to
somehow lock the methods on childform A, incase the user does a button
press at the same time as the 30sec poll occurs.

if so, whats the best aproach

What timer are you using?

If you're using the one from the Forms namespace, then the timer callback
(event handler) is executed on the form's thread, and doesn't need to be
synchronized with other code running on that same thread (such as code
that executes as a direct result of pushing a button control).

If you're using a different timer class, then it's not using the same
thread as your form and you do need to synchronize access to shared data.

If you're doing something funny like creating forms on a thread other than
your main thread, it's probably not quite this simple, but I'd guess
practically no one does that due to all the headaches it would cause. :)

Pete
 
P

Peted

What timer are you using?

Well the timer is a timer componet running on childform B. during its
cycle tyime it calls a method on childform A

The manual button clicks are buttons on childform A.

I assume childforms A and B run on different threads, but im not sure
about this, hence asking

do i need to do any sort of method lock, if a timer on childform B
access a method on child form A ?

Peted
 
W

Will

He means which library you are using for the Timer method.
C# has 3 different Timers
System.Windows.Form.Timer - a very basic timer. Most often used. Use
for client type applications and single-threaded places.
System.Timers.Timer - More advanced, can be used in things such as
window services. Use for server based programs.
System.Threading.Timer - each execution of the elasped method goes off
on its own thread. Endless amounts of fun if you have something that
kicks off alot of times.
 
P

Peter Duniho

Well the timer is a timer componet running on childform B. during its
cycle tyime it calls a method on childform A

The manual button clicks are buttons on childform A.

Assuming you are using the Timer component that you can drag onto your
form in the VS form designer, then that's the Timer from the Forms
namespace, and does not have any thread synchronization issues the way you
are using it.
I assume childforms A and B run on different threads, but im not sure
about this, hence asking

It's not true that different forms run on different threads. Each form
will run on the thread used to create it, and since normally you create
new forms from the same thread as the main form, any new form is run on
the same thread as your main form.

In most applications there is a single thread that handles all of the
GUI. You have to do extra work for this to not be true, so unless you
know for sure that your application is different, all of your forms are on
the same thread. :)
do i need to do any sort of method lock, if a timer on childform B
access a method on child form A ?

No locking should be required in this case.

Pete
 

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