Window freezing after Timer.Elapsed

G

Guest

Hi All,

I have a simple application that allows users to clock in and out and stores
the data for use by the payroll department. It spends most of its life as a
tray icon and when the user clicks on it, a clock-in/out form is displayed.
My problem is this:

I've added a timer to the main module to allow the user to set a time to be
reminded that they should clock back in/out. The timer works great (i.e. if
it's set to remind you at 5pm, it reminds you at 5pm), but when the elapsed
event fires and the clock-in/out form is displayed, the form elements don't
repaint themselves (it's just a frame with no components) and if you click
anywhere in the form, you get the windows error message that "the application
has stopped responding."

Here's the weird thing: The sub that I use to display the form after the
timer elapses is the same sub that I call when the user right-clicks the tray
icon and selects "Display Timeclock Form" and it works like a charm. I can
open and close the form all day long with the tray icon context menu, but
when I use the timer to display the form, it freezes.

Here's what I've got... simplified. Any insights would be greatly
appreciated!

Thanks!
Mike E.

Sub main()

reminderTimer = New Timer
reminderTimer.AutoReset = False

frmMain = New frmTimeTracker

'setup context menu
cMenu.MenuItems.Add("Open Time Tracker", AddressOf cMenu_Click)
cMenu.MenuItems.Add("Set / Reset Reminder Time", AddressOf
cMenu_Click)
cMenu.MenuItems.Add("Exit Time Tracker", AddressOf cMenu_Click)

'create tray icon
trayIcon.Text = Time Tracker"
trayIcon.Icon = frmMain.Icon
trayIcon.Visible = True
trayIcon.ContextMenu = cMenu

AddHandler reminderTimer.Elapsed, AddressOf OnTimerElapsed

Application.Run()

End Sub


Public Sub OnTimerElapsed(ByVal source As Object, ByVal e As
ElapsedEventArgs)
bReminderSet = False
reminderTimer.Enabled = False
reminderTimer.Close()
ShowTimecardForm()

End Sub

Private Sub ShowTimecardForm()
If (frmMain Is Nothing OrElse frmMain.IsDisposed) Then
frmMain = New frmTimeTracker
End If
frmMain.Show()
frmMain.txtPassword.Focus()
Application.DoEvents()
End Sub
 
J

Jared

It sounds like a threading issue. You really can't tell becuase you didn't
post the suspect code block. Is the timer handler in a different thread than
the displayed form? Did you use a delegate? I'm just guessing...
 
G

Guest

Hi Jared,

I didn't set up an explicit thread for the timer, as I was just using
Application.run to control the app. The timer handler is the OnTimerElapse
method in the main module. Here's the rest of the code from the main module-
hopefully it will help:

Private Sub trayIcon_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles trayIcon.DoubleClick
ShowTimecardForm()
End Sub

Private Sub cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim mi As MenuItem = DirectCast(sender, MenuItem)
Select Case (mi.Text)
Case "Open Time Tracker"
ShowTimecardForm()
Case "Set / Reset Reminder Time"
PromptReminder()
Case "Exit Time Tracker"
Application.Exit()
End Select
Catch ice As InvalidCastException
ErrorMessageToFile("Error in cMenu_Click: " + vbCrLf +
ice.ToString)
End Try

End Sub

Public Sub PromptReminder()
Dim Reminder As ReminderFrm = New ReminderFrm

reminderTimer.Enabled = False

Reminder.ShowDialog()
If (Reminder.DialogResult = DialogResult.OK) Then
ReminderTime = Reminder.cboTimes.Text.ToString()
Dim ts As TimeSpan
ts = Date.Parse(ReminderTime).Subtract(Now)
If (ts.TotalMilliseconds < 1) Then
Exit Sub
End If
reminderTimer.Interval = ts.TotalMilliseconds
bReminderSet = True
reminderTimer.Enabled = True
Else
'reminder cancelled
End If
End Sub
 
T

Think_Fast

Try this:

Application.Run(frmMain)


Mike Eaton said:
Hi Jared,

I didn't set up an explicit thread for the timer, as I was just using
Application.run to control the app. The timer handler is the
OnTimerElapse
method in the main module. Here's the rest of the code from the main
module-
hopefully it will help:

Private Sub trayIcon_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles trayIcon.DoubleClick
ShowTimecardForm()
End Sub

Private Sub cMenu_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim mi As MenuItem = DirectCast(sender, MenuItem)
Select Case (mi.Text)
Case "Open Time Tracker"
ShowTimecardForm()
Case "Set / Reset Reminder Time"
PromptReminder()
Case "Exit Time Tracker"
Application.Exit()
End Select
Catch ice As InvalidCastException
ErrorMessageToFile("Error in cMenu_Click: " + vbCrLf +
ice.ToString)
End Try

End Sub

Public Sub PromptReminder()
Dim Reminder As ReminderFrm = New ReminderFrm

reminderTimer.Enabled = False

Reminder.ShowDialog()
If (Reminder.DialogResult = DialogResult.OK) Then
ReminderTime = Reminder.cboTimes.Text.ToString()
Dim ts As TimeSpan
ts = Date.Parse(ReminderTime).Subtract(Now)
If (ts.TotalMilliseconds < 1) Then
Exit Sub
End If
reminderTimer.Interval = ts.TotalMilliseconds
bReminderSet = True
reminderTimer.Enabled = True
Else
'reminder cancelled
End If
End Sub
 
J

Jared

I don't see how that would make a difference (could be my inexperience
talking), the documentation states that application.run will start a message
loop on the current thread, whereas application.run(frmMain) does the exact
same thing, except it initially displays a form.

The documentation on DoEvents also states:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemwindowsformsapplicationclassdoeventstopic.htm
CAUTION Calling this method can cause code to be re-entered if a message
raises an event.
I am assuming you placed the DoEvents in your code after you started having
problems in order to repaint your form.

You are also using a System.Timers.Timer and not a
System.Windows.Forms.Timer, read the overviews of each they define the
idiosyncrasies of each control. It seems like the system.timers.timer is
meant for multi-threaded apps and the system.windows.forms.timer is meant
for use in sta windows apps such as this one.

As a suggestion, I would dispose of my forms when done, and create a new
instance when the user requests it (if the overhead isn't too great, etc.)
especially if there may be several hours in between uses. I have a post in
microsoft.public.dotnet.languages.vb.controls titled "vb.net browser
refreshing" dated on 9.10.2004 that show an example of using a few api calls
to set the forms focus. It may come in handy when you are displaying your
reminders.
 
T

Think_Fast

Forgot a couple of things. First make the form invisible.

frmMain.Visible=False
Application.Run(frmMain)


And put this in your closing event of your form otherwise the application
will terminate

Private Sub frmTimeTracker_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.Visible = False
e.Cancel = True
End Sub
 
C

Cor Ligthert

Mike,

When I see it right in your code, than you are creating everytime a new form
from the same class, try to work with visible true and false or something
like that and use the object you are in.

You can not instance an object as new and delete it in the same time the one
which is instancing it.

When I see it right you do now something as
frmmain
new frmmain
new frmmain
new frmmain
new frmmain

I hope this helps?

Cor
 

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