forms timers, events, and threading

B

Bob

My WinForms app runs on the single default thread, and uses a single
SqlConnection object for all queries. I need to use one or more timers to
periodically execute some of them. My own testing indicates that the forms
timer does not operate on its own thread and will not cause a query to be
sent to use the single SqlConnection while another is being executed from
the handling of other events. Is that right? I just want to be 100% sure
before I commit to this design.

Bob
 
C

Cor Ligthert

Bob,

I am not completly sure of your question, however a thread does only one
operation per operation. So when there is a longtime taking SQL operation
by instance a dataadapter update, than that will first be done, before the
timer event is catched.

I hope this helps?

Cor
 
C

Cor Ligthert

Bob,

However before I forget it, why do you not test this kind of questions
yourself when it is so important. A small test program is in my opinon fast
enough made?

sub Myprocedure Y
threading.thread.sleep(4000)
end sub

sub Timer
test if it is throw by setting the timertick to 1000 miliseconds
end sub

Cor
 
B

Bob

I was wrong. 100% sure just wen to 0%.

OK, new question. How can I get message boxes and other modal dialogues to
stop timer tick events just like the interventing execution of other
event-driven code code?

I guess I'll have no choice now but to brew my own message boxes. What a
pain.

Bob
 
K

Kejpa

No, 100% sure of having right switched to 100% sure of having wrong. The
100% sureness is there all the time <LOL>

Can't you just before showing the message set the timer.enabled = false and
reset it after showing the messagebox?
I do that, but I'm using system.timer....
Maybe you should switch

HTH
/Kejpa
 
C

Cor Ligthert

Bob,

For which timer do you want to know this?

Windows.forms.timer
or
System.timers.timer
or
System.threading.timer
or
Another one

Cor
 
B

Bob

Cor and Kejpa,

I really don't care what kind of timers I'm using, the only thing I'm after
is that they don't tick while code is executing doing other events.
Unfortunately I'm willing to bet none of them stop when a message box is
popped up. That's what I'd like, but I'm probably just going to have to
subclass all my timers, then make a custom message box class, and have the
two classes communicate with static methods. I want to avoid adding this
level of complexity if I can help it, but I'm not sure there's any other
way.

Bob
 
C

Cor Ligthert

Bob,

Complexity starts mostly that people do not know what they want to archieve
when they can tell that in a simple way, than mostly it is easy to get an
easy procedure as well. As far as I hear you, it seems an easy problem to
solve.

Cor

"Bob" <[email protected]>

....
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
Didn't you ask this question on Sept 29th 2004?

http://groups.google.com/groups?hl=...&selm=ei6CKhipEHA.3520%40TK2MSFTNGP11.phx.gbl

I'm not sure if these threads will help you or not:

http://groups.google.com/groups?hl=...=pmuql010tdkdckh7es1foth98d89i9k8eb%404ax.com

http://groups.google.com/groups?hl=...elm=uT%24SckKHEHA.1272%40TK2MSFTNGP12.phx.gbl


Rather then brew your own message box, I would have a well known object (a
Singleton, either the MainForm or my ApplicationContext singleton) have a
pair of methods that "locked" out the Timer events. Alternatively I would
code the Timer events so they were not reentrant (by checking a Static
variable in the method, the third link above has a sample).

Normally I simply code my Timer events so they are not reentrant.

Hope this helps
Jay
 
C

Cor Ligthert

Bob,

Maybe can this sample that I made for you clear it better.

\\\Needs one button on a form
Private start As Integer
Private WithEvents mytimer As New Windows.Forms.Timer
Private Sub Button1_Click(ByVal sender _
As Object, ByVal e As System.EventArgs) _
Handles Button1.Click
mytimer.Enabled = True
mytimer.Interval = 2000
start = Environment.TickCount
Threading.Thread.Sleep(1000)
Threading.Thread.Sleep(1000)
Threading.Thread.Sleep(1000)
Threading.Thread.Sleep(1000)
End Sub

Private Sub mytimer_Tick(ByVal sender As Object, ByVal _
e As System.EventArgs) Handles mytimer.Tick
mytimer.Enabled = False
MessageBox.Show((Environment.TickCount - _
start).ToString)
End Sub
///
 
C

Cor Ligthert

Bob,

Because that Jay showed us that previous thread, I think that I could make a
better sample.

\\\
Private WithEvents mytimer As New Windows.Forms.Timer
Private Sub Form4_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
mytimer.Enabled = True
mytimer.Interval = 5000
End Sub
Private Sub mytimer_Tick(ByVal sender As Object, ByVal _
e As System.EventArgs) Handles mytimer.Tick
Dim start As Integer
mytimer.Enabled = False
start = Environment.TickCount
MessageBox.Show((Now.ToString("mm-ss")))
mytimer.Interval = 5000 - _
(Environment.TickCount - start)
mytimer.Enabled = True
start = Environment.TickCount
End Sub
///
You have to click inside the 5 seconds for this button, and ending the
program can give an error however it is just to show you this.

This accoording what I now understand from your problem. Know that you never
can use in Net1.1 one ADONET connection more than one time.

I hope this helps?

Cor
 
B

Bob

Yes, I posted a message on a previous "thread" (pun intended) but it was
more about whether or not to use threading at all. I've pretty much settled
on not using threading if I can help it, but ran into the message box
problem in the middle of finally committing to the single-thread design.
Unfortunately blocking reentry on the instance level is not enough, there
may be many timers; I'm thinking of using a static property. That takes me
90% of the way there.... actually that might be enough and I might not have
to worry about subclassing message boxes at all.

Thanks for helping me think a bit.
Bob
 
J

Jay B. Harlow [MVP - Outlook]

Bob,
may be many timers; I'm thinking of using a static property.
A static property of what?

In the case of many timers, I would consider making a static (Shared)
property of a custom Timer class. That enabled raising Timer.Tick events.

Something like:

Public Class TimerEx
Inherits System.Windows.Forms.Timer

Private Shared m_eventsEnabled As Boolean

Public Shared Property EventsEnabled() As Boolean
Get
Return m_eventsEnabled
End Get
Set(ByVal value As Boolean)
m_eventsEnabled = value
End Set
End Property

Protected Overrides Sub OnTick(ByVal e As System.EventArgs)
If m_eventsEnabled Then
MyBase.OnTick(e)
End If
End Sub

End Class

Then on my forms I would use a TimerEx instead of Timer.

When TimerEx.EventsEnabled is False, no timer will raise its events, if
TimerEx.EventsEnabled is True then all timers will raise their events.

However! I normally favor Threading over using Timers to mimic Threading.

Hope this helps
Jay
 

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

Similar Threads


Top