By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,165 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

when is threading better than a timer?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.