473,372 Members | 863 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,372 software developers and data experts.

Window freezing after Timer.Elapsed

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
Nov 21 '05 #1
7 2651
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...

"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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

Nov 21 '05 #2
Try this:

Application.Run(frmMain)
"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
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

"Jared" wrote:
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...

"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
> 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
>
>


Nov 21 '05 #3
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/frlrfsystemwindowsformsapplicationclassdoeventstop ic.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.

"Think_Fast" <nospam@haha> wrote in message
news:10*************@corp.supernews.com...
Try this:

Application.Run(frmMain)
"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
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

"Jared" wrote:
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...

"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
> 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
>
>


Nov 21 '05 #4
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
"Think_Fast" <nospam@haha> wrote in message
news:10*************@corp.supernews.com...
Try this:

Application.Run(frmMain)
"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
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

"Jared" wrote:
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...

"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
> 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
>
>


Nov 21 '05 #5
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
Nov 21 '05 #6
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

"Jared" wrote:
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...

"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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


Nov 21 '05 #7
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

"Jared" wrote:
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...

"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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


Nov 21 '05 #8

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

Similar topics

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...
2
by: Besta | last post by:
Hello all, I am having trouble creating a windows service with a timer. Everything seems to go ok but the elapsed event does not fire.Can anyone shed any light on this, may be something simple as...
11
by: Philip Wagenaar | last post by:
Hello, I am using a timer object in my Windows Forms Application. Does the code in ..elapsed event run in a diffrent thread? If the interval is set to 10 milliseconds and the time to execute the...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
12
by: martin1 | last post by:
All, is there window form refresh property? I try to set up window form refresh per minute. Thanks
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
10
by: igor | last post by:
I have recently discovered that the system.Timers.Timer from.Net Framework v1.1 is not reliable when used on Windows 2003 server. When incorporated into a Windows Service, the timer_elapsed event...
4
by: Boki | last post by:
Hi All, I have a timer, if my data queue Q has data, the timer should start work, if there is no data in Q, the timer should stop. However, there is an event can fire timer to start. Should I...
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.