Search code examples
vb.nettoastsystem.timers.timer

Issues Using ToastForms With System.Timers.Timer


In my application I have made use of a ToastForms class to show popup notifications to the user for live alerts. I have been trying to workout the best method of polling alerts from the database and presenting them to the user on a frequent basis. I query the database, find all new alerts and then present these to the user as a popup every 10 or so seconds. I have been trying to decide the best method/practise for this procedure as I don't want to cause any high CPU usage or program hangs as I constantly poll the database.

After experimenting a little, I decided to move my thoughts to using a System.Timers.Timer and place my code inside a Get_Alerts procedure:

Private Sub frmNewDashboard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   ...
   tmr_GetAlerts = New System.Timers.Timer(10000)
   AddHandler tmr_GetAlerts.Elapsed, AddressOf GetAlerts
   tmr_GetAlerts.AutoReset = False
   tmr_GetAlerts.Start()
   ...
End Sub

Private Sub GetAlerts(source As Object, e As ElapsedEventArgs)
   ...
   'Query the database and populate a datatable
   'Determine all alert types
   'Handle Major Alerts, Minor Alerts, etc.
   If (Current_User.EnableNotifications = True) And (Current_User.NP_AMajor = True) Then
      If MajorCount > 1 Then
         Dim slice As ToastForm
         slice = New ToastForm((Current_User.Notify_Seconds * 1000), MajorCount & " New Major Alert(s) Detected")
         slice.Height = 100
         slice.Show()
      End If
   End If
   ...
   'Same code repeats to handle Minor Alerts
End Sub

The above code used to work fine on a normal Forms.Timer, however, since moving it to a System.Timers.Timer I am finding that the ToastForm will popup fine but then seems to hang and never closes:

enter image description here

It's not producing any errors so I'm unsure where the fault lies. I'm assuming it's something to do with opening the ToastForm on a different thread to my Timer, but I'm not sure.

Any help would be appreciated. Thanks.

Update Below is the code that runs the Toastform. I have imported the class from some code I found on the net so it is not my code. I just pass in the arguments. It was all working fine (and closing) until I introduced the System.Timers.Timer.

Imports System.Runtime.InteropServices

Public Class ToastForm

    Private _item As ListViewItem = Nothing
    Private TooltipVisible As Boolean = False
    Private SelectedCallQueue As String = Nothing
    Private SelectedOverduePeriod As String
    Private OnlineUserCount As Integer

#Region " Variables "

    ''' <summary>
    ''' The list of currently open ToastForms.
    ''' </summary>
    Private Shared openForms As New List(Of ToastForm)

    ''' <summary>
    ''' Indicates whether the form can receive focus or not.
    ''' </summary>
    Private allowFocus As Boolean = False
    ''' <summary>
    ''' The object that creates the sliding animation.
    ''' </summary>
    Private animator As FormAnimator
    ''' <summary>
    ''' The handle of the window that currently has focus.
    ''' </summary>
    Private currentForegroundWindow As IntPtr

#End Region 'Variables

#Region " APIs "

    ''' <summary>
    ''' Gets the handle of the window that currently has focus.
    ''' </summary>
    ''' <returns>
    ''' The handle of the window that currently has focus.
    ''' </returns>
    <DllImport("user32")> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function

    ''' <summary>
    ''' Activates the specified window.
    ''' </summary>
    ''' <param name="hWnd">
    ''' The handle of the window to be focused.
    ''' </param>
    ''' <returns>
    ''' True if the window was focused; False otherwise.
    ''' </returns>
    <DllImport("user32")> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function

#End Region 'APIs

#Region " Constructors "

    ''' <summary>
    ''' Creates a new ToastForm object that is displayed for the specified length of time.
    ''' </summary>
    ''' <param name="lifeTime">
    ''' The length of time, in milliseconds, that the form will be displayed.
    ''' </param>
    Public Sub New(ByVal lifeTime As Integer, ByVal message As String)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.


        'Set the time for which the form should be displayed and the message to display.
        Me.lifeTimer.Interval = lifeTime
        Me.messageLabel.BackColor = ColorTranslator.FromHtml(Current_User.NWC)
        Me.messageLabel.Text = message

        'Display the form by sliding up.
        Me.animator = New FormAnimator(Me, _
                                       FormAnimator.AnimationMethod.Slide, _
                                       FormAnimator.AnimationDirection.Up, _
                                       200)
    End Sub

#End Region 'Constructors

#Region " Methods "

    ''' <summary>
    ''' Displays the form.
    ''' </summary>
    ''' <remarks>
    ''' Required to allow the form to determine the current foreground window     before being displayed.
    ''' </remarks>
    Public Shadows Sub Show()
        Try
            'Determine the current foreground window so it can be reactivated each time this form tries to get the focus.
            Me.currentForegroundWindow = GetForegroundWindow()

            'Display the form.
            MyBase.Show()
            'Play a notification sound
            If Current_User.NotifySound = True Then NotificationSound.Play()

        Catch ex As Exception
            ErrorTrap(ex, "ToastForm: Show()")
        End Try
    End Sub

#End Region 'Methods

#Region " Event Handlers "

    Private Sub ToastForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Display the form just above the system tray.
            Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width - Me.Width - 5, _
                                    Screen.PrimaryScreen.WorkingArea.Height - Me.Height - 5)

            'Move each open form upwards to make room for this one.
            For Each openForm As ToastForm In ToastForm.openForms
                openForm.Top -= Me.Height + 5
            Next

            'Add this form from the open form list.
            ToastForm.openForms.Add(Me)

            'Start counting down the form's liftime.
            Me.lifeTimer.Start()

        Catch ex As Exception
            ErrorTrap(ex, "ToastForm: ToastForm_Load()")
        End Try
    End Sub

    Private Sub ToastForm_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Try
            'Prevent the form taking focus when it is initially shown.
            If Not Me.allowFocus Then
                'Activate the window that previously had the focus.
                SetForegroundWindow(Me.currentForegroundWindow)
            End If

        Catch ex As Exception
            ErrorTrap(ex, "ToastForm: ToastForm_Activated()")
        End Try
    End Sub

    Private Sub ToastForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Try
            'Once the animation has completed the form can receive focus.
            Me.allowFocus = True

            'Close the form by sliding down.
            Me.animator.Direction = FormAnimator.AnimationDirection.Down

        Catch ex As Exception
            ErrorTrap(ex, "ToastForm: ToastForm_Shown()")
        End Try
    End Sub

    Private Sub ToastForm_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs) Handles MyBase.FormClosed
        Try
            'Move down any open forms above this one.
            For Each openForm As ToastForm In ToastForm.openForms
                If openForm Is Me Then
                    'The remaining forms are below this one.
                    Exit For
                End If

                openForm.Top += Me.Height + 5
            Next

            'Remove this form from the open form list.
            ToastForm.openForms.Remove(Me)

        Catch ex As Exception
            ErrorTrap(ex, "ToastForm: ToastForm_FormClosed()")
        End Try
    End Sub

    Private Sub lifeTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles lifeTimer.Tick
        Try
            'The form's lifetime has expired.
            Me.Close()

        Catch ex As Exception
            ErrorTrap(ex, "ToastForm: lifeTimer_Tick()")
        End Try
    End Sub

#End Region 'Event Handlers
End Class

Solution

  • I think that the solution here is probably to create the notification forms on the UI thread. You can still do the query to get the data on a secondary thread but then marshal a method call to the UI thread to display the notifications.

    One simple option for that is to use a Windows.Forms.Timer instead of a Timers.Timer and call RunWorkerAsync on a BackgroundWorker in the Tick event handler. You can then do the query in the DoWork event handler, which is executed on a secondary thread, and display the notifications in the RunWorkerCompleted event handler, which is executed on the UI thread.