troubleshooting Question

Eventhandler tips

Avatar of AWestEng
AWestEngFlag for Sweden asked on
.NET ProgrammingVisual Basic.NETEditors IDEs
23 Comments1 Solution1004 ViewsLast Modified:
Hi..

Need some help on a eventhandler
(It should work like this, the program imports the eventhandler dll, the application adds all the events to the eventhandler and the events fires every xx seconds when the evethandler is started)

This is the pseudocode

Any tips how to do this in a better way?
Is it thread safe?
Do I need to use delegete?
And so on.....

And I tried the code but I a NullReferenceException on the obj.[GenerateEvents]() row
Please check the code and see if I can make it any better.

Thx guys.


Imports System
Imports System.Timers
Imports System.Collections
 
Public Class EventHandler
 
    Private Shared AllInstance As New Collection()
    Private Shared m_Timer As New System.Timers.Timer
 
       '// Register the object
    Public Shared Sub Register(ByVal obj As Object)
        [m_AllInstances].Add(obj)
    End Sub
 
    '// Start the events
    Public Shared Sub StartEvents(ByVal Frequency As Double)
 
        '// Start the timer if it's not already running
        If Not m_Timer.Enabled Then
            '// Set the Interval(milliseconds).
            Dim milliseconds As Integer = (1 / Frequency) * 1000
 
            '// Add the Elapsed event for the timer.
            AddHandler m_Timer.Elapsed, AddressOf OnTimedEvent
 
            m_Timer.Interval = milliseconds
            m_Timer.Enabled = True
            m_Timer.Start()
            ' Keep the timer alive until the end of Main.
            'GC.KeepAlive(aTimer)
        End If
    End Sub
 
 
    '// Stop the events
    Public Shared Sub StopEvents()
 
        '// Start the timer if it's not already running
        If m_Timer.Enabled Then
            '// Disable timer
            m_Timer.Enabled = False
 
            '// Remove the Elapsed event for the timer.
            RemoveHandler m_Timer.Elapsed, AddressOf OnTimedEvent
        End If
    End Sub
 
    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        For Each obj As Object In m_AllInstances
            obj.[GenerateEvents]()
        Next
    End Sub
 
End Class
 
Public Class MyApp
 
    Private Event OnChange()
 
    Public Sub New()
        EventHandler.Register(Me)
    End Sub
 
    Public Sub GenerateEvents()
 
        If True Then
            RaiseEvent OnChange()
        End If
 
    End Sub
 
    Private Sub GetData() Handles Me.OnChange
        'Get data
    End Sub
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 23 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros