473,396 Members | 1,982 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,396 software developers and data experts.

when is threading better than a timer?

Bob
Okay, I've done this for years but now I'm going to question it just because
this idea has been at the back of my head since I started using DotNet...

My WinForms app queries a database every 60 seconds. I set up a singleton
class that's instantiated when the app starts, and that starts a timer. My
question is, would it be better to run this checking process on a separate
thread and have it use its own DB connection? If there isn't a clear yes or
no, what circumstances affect the answer?

TIA,
Bob
Nov 21 '05 #1
10 1631
Bob,

In my opinion it is like this, for ever when you do not need threading don't
use it, the OS should keep eyes on the threads so it will be only extra
processing time. Extra threads are very usefull when you can let 2 processes
go in the same time, which have both wait moments, so they can go both go on
while the other is waiting. A very good example for that is downloading.

And your sample, when you use a timer, your application does nothing than
wait on an event. For the rest it is stopped. Do you want to let it two
times wait on events?

However just my thought.

Cor
"Bob" <no***@nowhere.com>
Okay, I've done this for years but now I'm going to question it just
because
this idea has been at the back of my head since I started using DotNet...

My WinForms app queries a database every 60 seconds. I set up a singleton
class that's instantiated when the app starts, and that starts a timer. My
question is, would it be better to run this checking process on a separate
thread and have it use its own DB connection? If there isn't a clear yes
or
no, what circumstances affect the answer?

TIA,
Bob

Nov 21 '05 #2
Bob,
If you have a long running process (half a minute or longer) I would
recommend a separate thread. By long running, I mean if it takes long enough
that the user will start to think your app stopped responding (hung).

If the database check takes fractions of a second I would leave it as a
Windows Timer.

Remember there are three distinct timer objects in .NET. One of the other
two may be more appropriate, the following articles in MSDN Magazine explain
the difference between the three timer objects in .NET & when to use each.

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx

The above articles also discusses if & how each timer interacts with
threading.

Hope this helps
Jay
Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:ei**************@TK2MSFTNGP11.phx.gbl...
Okay, I've done this for years but now I'm going to question it just
because
this idea has been at the back of my head since I started using DotNet...

My WinForms app queries a database every 60 seconds. I set up a singleton
class that's instantiated when the app starts, and that starts a timer. My
question is, would it be better to run this checking process on a separate
thread and have it use its own DB connection? If there isn't a clear yes
or
no, what circumstances affect the answer?

TIA,
Bob

Nov 21 '05 #3
Bob
It's milliseconds. Looks like I was using the right Timer class, too
(System.Windows.Forms.Timer), since I wanted it to mesh with and respect the
UI. Very good links, thank you.

Bob

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OF****************@TK2MSFTNGP10.phx.gbl...
Bob,
If you have a long running process (half a minute or longer) I would
recommend a separate thread. By long running, I mean if it takes long enough that the user will start to think your app stopped responding (hung).

If the database check takes fractions of a second I would leave it as a
Windows Timer.

Remember there are three distinct timer objects in .NET. One of the other
two may be more appropriate, the following articles in MSDN Magazine explain the difference between the three timer objects in .NET & when to use each.

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx

The above articles also discusses if & how each timer interacts with
threading.

Hope this helps
Jay
Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:ei**************@TK2MSFTNGP11.phx.gbl...
Okay, I've done this for years but now I'm going to question it just
because
this idea has been at the back of my head since I started using DotNet...
My WinForms app queries a database every 60 seconds. I set up a singleton class that's instantiated when the app starts, and that starts a timer. My question is, would it be better to run this checking process on a separate thread and have it use its own DB connection? If there isn't a clear yes
or
no, what circumstances affect the answer?

TIA,
Bob


Nov 21 '05 #4
Jay,

I hear that blocking of the UI by using a timer now for a long time, who has
stated this the first time. There is nothing wrong with multithreading
however maybe you can see what I wrote in the message above.

To proof what I wrote I made this snippet, it needs only a form and a
button.

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer
timer1.Enabled = True
timer1.Interval = 4000
End Sub
Public Sub mytimer(ByVal sender As Object, _
ByVal e As System.EventArgs)
MessageBox.Show("Jay the timer event is fired however everything
goes on")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Jay there is nothing blocked at all")
End
///

I use multithreading by the way almost from the first day I used VBNet.

I am curious for your answer.

Cor
Nov 21 '05 #5
Cor,
I hear that blocking of the UI by using a timer now for a long time, who
has stated this the first time. I stated that handling of the Timer can block the UI, you have shown that
the UI, more specifically MessageBox, does not block a Timer or the UI. I'm
sure MessageBox doesn't block, as I suspect MessageBox.Show has a message
pump (aka equivalent of Application.DoEvents), which ensures the underlying
window is repainted as needed...

Instead of displaying message boxes, try putting time consuming code in both
methods.

Private Sub DoStuff(ByVal value As Double)
System.Threading.Thread.Sleep(TimeSpan.FromMinutes (value))
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Button1.BackColor = Color.Red
Button1.Refresh()
DoStuff(1)
Button1.BackColor = SystemColors.Control
Button1.Refresh()
MessageBox.Show("Jay the timer event is fired however everything
goes on")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.BackColor = Color.Red
Me.Refresh()
DoStuff(1)
Me.BackColor = SystemColors.Control
Me.Refresh()
MessageBox.Show("Jay there is nothing blocked at all")
End Sub

You can use a Loop to simulate work...

Private Sub DoStuff(ByVal value As Double)
Dim current As TimeSpan = DateTime.Now.TimeOfDay
Dim finish As TimeSpan = current.Add(TimeSpan.FromMinutes(value))
Do Until TimeSpan.Compare(current, finish) > 0
current = DateTime.Now.TimeOfDay
Loop
End Sub

Be certain to look at your app under Task Manager, to verify it is "Not
Responding".
I use multithreading by the way almost from the first day I used VBNet. Is there a point here? I've been using mutli-threading since the early 80s,
does either make a real difference?

Hope this helps
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ee**************@tk2msftngp13.phx.gbl... Jay,

I hear that blocking of the UI by using a timer now for a long time, who
has stated this the first time. There is nothing wrong with multithreading
however maybe you can see what I wrote in the message above.

To proof what I wrote I made this snippet, it needs only a form and a
button.

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
AddHandler timer1.Tick, AddressOf mytimer
timer1.Enabled = True
timer1.Interval = 4000
End Sub
Public Sub mytimer(ByVal sender As Object, _
ByVal e As System.EventArgs)
MessageBox.Show("Jay the timer event is fired however everything
goes on")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Jay there is nothing blocked at all")
End
///

I use multithreading by the way almost from the first day I used VBNet.

I am curious for your answer.

Cor

Nov 21 '05 #6
Jay,
Instead of displaying message boxes, try putting time consuming code in
both methods.


That is with every procedure, not only when there is a timer involved and
than you have to take the choise what to do.

However is no decission Timer or Thread, what I have the last time seen very
much mixed up in the dotNet newsgroups.

However maybe you can correct me in this?

Cor.


Nov 21 '05 #7
Cor,
That is with every procedure, not only when there is a timer involved and
than you have to take the choise what to do. I believe you missed the point!!

In your sample, you used a MessageBox.Show in what I perceived to be an
attempt to prove that Windows Timers do not block the UI & the UI does not
block Windows Timers. My code demonstrates that using MessageBox.Show in the
example was a bad idea! That you need to actually execute code.

When you actually execute code then you can clearly see that Windows Timers
do indeed block the UI & the UI does indeed block Windows Timers.

Of course if you were attempting to demonstrate something else, then my
sample is moot and this discussion is moot. We could start over if you care
to better explain what you were attempting to demonstrate!
However is no decission Timer or Thread, what I have the last time seen
very much mixed up in the dotNet newsgroups. I'm sorry this sentence is very mixed up, I am not at all following what you
are attempting to say. Can you explain it better?
However maybe you can correct me in this? I just did, see the post you just responded to, try the code.

Hope this helps
Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ug**************@TK2MSFTNGP14.phx.gbl... Jay,
Instead of displaying message boxes, try putting time consuming code in
both methods.


That is with every procedure, not only when there is a timer involved and
than you have to take the choise what to do.

However is no decission Timer or Thread, what I have the last time seen
very much mixed up in the dotNet newsgroups.

However maybe you can correct me in this?

Cor.

Nov 21 '05 #8
Jay B,

Maybe I can show you with the combined samples beneath what I want to say.
When you have time for it, than try it, it looks nice.
However when you see it, you understand it as well in my opinion.

Beneath it is my comment what I want to show with it.

\\\It needs on a form 3 buttons and a textbox
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
TextBox1.Text = "0"
TextBox2.Text = "0"
TextBox3.Text = "0"
AddHandler timer1.Tick, AddressOf mytimer1
timer1.Enabled = True
timer1.Interval = 4000
Dim timer2 As New System.Windows.Forms.Timer
AddHandler timer2.Tick, AddressOf mytimer2
timer2.Enabled = True
timer2.Interval = 300
Dim timer3 As New System.Windows.Forms.Timer
AddHandler timer3.Tick, AddressOf mytimer3
timer3.Enabled = True
timer3.Interval = 20
End Sub
Public Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Public Sub mytimer2(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox2.Text = (CInt(TextBox2.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Public Sub mytimer3(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox3.Text = (CInt(TextBox3.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim current As TimeSpan = DateTime.Now.TimeOfDay
Dim finish As TimeSpan = current.Add(TimeSpan.FromSeconds(10))
Do Until TimeSpan.Compare(current, finish) > 0
current = DateTime.Now.TimeOfDay
Loop
End Sub
///

What I want to say with is, that it is not the timer that needs
multithreading (or whatever) it is the proces, which can be to long with or
without a timer.

Therefore it is not when is threading better than a timer (And that is the
subject of this thread).

That has nothing to do with each other.

Cor
Nov 21 '05 #9
Cor,
What I want to say with is, that it is not the timer that needs
multithreading (or whatever) it is the proces, which can be to long with
or without a timer. Gee, isn't that what I stated in my original response to Bob?

<quote>
If you have a long running process (half a minute or longer) I would
recommend a separate thread. By long running, I mean if it takes long enough
that the user will start to think your app stopped responding (hung).
</quote>

(shakes head, walks off mutter to himself).

Jay

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl... Jay B,

Maybe I can show you with the combined samples beneath what I want to say.
When you have time for it, than try it, it looks nice.
However when you see it, you understand it as well in my opinion.

Beneath it is my comment what I want to show with it.

\\\It needs on a form 3 buttons and a textbox
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim timer1 As New System.Windows.Forms.Timer
TextBox1.Text = "0"
TextBox2.Text = "0"
TextBox3.Text = "0"
AddHandler timer1.Tick, AddressOf mytimer1
timer1.Enabled = True
timer1.Interval = 4000
Dim timer2 As New System.Windows.Forms.Timer
AddHandler timer2.Tick, AddressOf mytimer2
timer2.Enabled = True
timer2.Interval = 300
Dim timer3 As New System.Windows.Forms.Timer
AddHandler timer3.Tick, AddressOf mytimer3
timer3.Enabled = True
timer3.Interval = 20
End Sub
Public Sub mytimer1(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox1.Text = (CInt(TextBox1.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Public Sub mytimer2(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox2.Text = (CInt(TextBox2.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Public Sub mytimer3(ByVal sender As Object, _
ByVal e As System.EventArgs)
TextBox3.Text = (CInt(TextBox3.Text) + 1).ToString
DirectCast(sender, System.Windows.Forms.Timer).Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim current As TimeSpan = DateTime.Now.TimeOfDay
Dim finish As TimeSpan = current.Add(TimeSpan.FromSeconds(10))
Do Until TimeSpan.Compare(current, finish) > 0
current = DateTime.Now.TimeOfDay
Loop
End Sub
///

What I want to say with is, that it is not the timer that needs
multithreading (or whatever) it is the proces, which can be to long with
or without a timer.

Therefore it is not when is threading better than a timer (And that is the
subject of this thread).

That has nothing to do with each other.

Cor

Nov 21 '05 #10
Jay,
<quote>
If you have a long running process (half a minute or longer) I would
recommend a separate thread. By long running, I mean if it takes long
enough
that the user will start to think your app stopped responding (hung).
</quote>

About that we have no misunderstanding at all, however I try all the time to
show that that has nothing to do with a timer, that is forever.

Cor

Nov 21 '05 #11

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

Similar topics

1
by: Gitte Wange | last post by:
Hello, I'm trying to create an application that needs a Timer() to do various checks. My class has a self.timer = Timer(900, self.sendDNSRequest) The timer itself works fine but whenever I try...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
19
by: Tiraman | last post by:
Hi , I have an assembly that hold few class and few imports which are not used in all of the class's . so my question is when to use the "Imports XXX" And when to use this kind of statement...
2
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an...
2
by: genojoe | last post by:
In simplified form, I have an RTF control and a timer. I use the timer to do an autoresave for the RFT control every 5 minutes. Here is the abridged code. Private Sub tmrAutoRecover_Tick(ByVal...
8
by: TheMadHatter | last post by:
Yello, Quick Q: I created a windows service that changes some data every (approx) 1sec, but currently it is using a loop on a separate thread that uses Thread.Sleep(1000). Is that bad...
4
by: grayaii | last post by:
Hi, I have a simple form that handles all its paint functionality like so: this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); And the entry point to this...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
0
by: Bruce | last post by:
Hi I have a question on using System.Threading.Timer. I understand that in order for the timer to work, I need to store a reference to the timer so that it does not get garbage collected. In my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.