Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
private void StopTimer(System.Timers.Timer myTimer, ElapsedEventHandler onIntervalElapsed)
        {
            //Derive onIntervalElapsed from myTimer and store in variable.
            //var propertyFromNonPublicMembers = e.g. myTimer.GetProperty("onIntervalElapsed");
            

            if (myTimer != null)
            {
                if (myTimer.Enabled == true)
                    myTimer.Enabled = false;

                try
                {
                    myTimer.Elapsed -= onIntervalElapsed;
                    //myTimer.Elapsed -= propertyFromNonPublicMembers;
                }
                catch (Exception)
                {
  
                }
                finally
                {
                    myTimer.Dispose();
                    myTimer = null;
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + $" Timer {myTimer} stopped");
                }
            }
        }


What I have tried:

Currently passing System.Timers.Timer and ElapsedEventHandlerTried to method StopTimer

Wondering if it is possible to obtain "onIntervalElapsed" value from myTimer in order to unsubscribe from the Timer.Elapsed event and therefore the need of passing the ElapsedEventHandler onIntervalElapsed to the method in the first place.
Posted
Updated 1-May-20 11:50am
v2
Comments
Richard MacCutchan 1-May-20 10:11am    
Does the documentation show that as one of the properties of a timer?

1 solution

System.Timers.Timer is "... for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime." MSDN ... Why did you choose this Timer from the 4 types of Timers available ?

Why do you need to get the 'ElapsedEventHandler at run-time ? Why not just define it, and use the reference to it, like this:
System.Timers.Timer myTimer = new System.Timers.Timer();

private void MyTimerOnElapsed(object sender, ElapsedEventArgs e)
{
    // whatever
}

private void StopTimer()
{
    myTimer.Stop();
    myTimer.Elapsed -= MyTimerOnElapsed;
    myTimer.Dispose();
    myTimer = null;
    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + $" Timer {myTimer} stopped");
}
 
Share this answer
 
Comments
Member 12014273 5-May-20 15:41pm    
There are multiple timers defined, running at different intervals, within the form application that I am using. Each timer has its own += ElapsedEventHandler defined.

Instead of writing a method to stop each timer individually, I came up with this solution that can be called instead.

It works, but was wondering if I can eliminate the need of passing the ElapsedEventHandler for each particular timer.
BillWoodruff 5-May-20 16:54pm    
Hi, Yet your code only deactivates the ElapsedEventHandler for one Timer. By the way: you can set an EventHandler to 'null, and that will not throw an error if the Event has no handlers.

myTimer.Elapsed = null; // see if this works in youur code

the tricky part is that any one Event can have multiple handlers: I don't know if that is relevant to your code.

cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900