473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
May 18 '07 #1
5 3000
On Thu, 17 May 2007 23:38:50 -0700, <Petedwrote:
[...]
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
May 18 '07 #2
>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
May 18 '07 #3
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.

May 18 '07 #4
On Fri, 18 May 2007 00:44:16 -0700, <Petedwrote:
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
May 18 '07 #5
Thanks very much

excelent info thanks heaps

Peted
On Fri, 18 May 2007 14:38:50 +0800, Peted wrote:
>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
May 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Evan David Light | last post by:
After agonizing over this problem for a few days, I've decided to seek help. No, not the variety that involes a jacket that zips up the back but this august body of intrepid individuals. I've...
3
by: rhaley | last post by:
ugh. Okay, I need to figure out the number of milliseconds between DateTime.Now and the end of the day so that I can make changes based on the date roll-over. I need to implement a Timer so that I...
9
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it,...
4
by: Ben | last post by:
Hello everybody I got confused by this problem for which I don't have a logical explanation. There is a Thread (ThreadA) which receives Events from another system thread (ThreadS). ThreadA then...
6
by: semkaa | last post by:
Can you explain why using ref keyword for passing parameters works slower that passing parameters by values itself. I wrote 2 examples to test it: //using ref static void Main(string args) {...
4
by: Lauren Quantrell | last post by:
I have just put together a vb.net app and now need to provide it to users. This application needs to run the code in a sub every 60 seconds from a Windows Service application. I have the...
2
by: Amit Dedhia | last post by:
Hi I am developing a scientific application which has moderate level image processing involved. In my application, there is a main application form which invokes another form. When this form...
12
by: Zytan | last post by:
I have a Timer class set to trigger every second. The Tick function that is called every second uses a lock to prevent multiple ticks from executing the same code at the same time. The code...
6
by: Curious | last post by:
I have an arraylist used in three separate methods. In method #1 (event method), some items are removed from the arraylist if certain conditions are met; In method #2 (event method),...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.