?
Solved

C# Remove Elapsed Event Handler From Timer Object

Posted on 2012-08-12
2
Medium Priority
?
3,134 Views
Last Modified: 2012-08-12
I'm doing some server side programming and trying to REUSE a System.Timers.Timer object without luck.

Here is what I want to be able to accomplish

 
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.AutoReset = false;
myTimer.Elapsed += delegate { OnMyTimer_Elapsed1(arg1,arg2);};//Yes I need to pass some args which are optional by the way

//then do some processing (this may take 500ms to 2000 ms)

If(something == true){
    //remove previous elapsed event handler from myTimer object
    myTimer.Elapsed -= delegate { OnMyTimer_Elapsed1();};//Notice -=

    //Then reattach different elapsed event handler
    myTimer.Elapsed += delegate { OnMyTimer_Elapsed2(arg1,arg2);}; 
}

Open in new window

This code compiles fine but OnMyTimer_Elapsed1 event is firing TWICE when “something == true” where I was expecting that to be fired only ONCE in any case.

As you can see I'm trying to remove the event handler with "-=" without luck. Your assistance is much appreciated! thanks.
0
Comment
Question by:ExcUsr2008
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38285438
Delegates are themselves objects. When you use the delegate keyword, you are creating a new object. You should maintain a scoped reference to the original handler. Try:

System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.AutoReset = false;
Action handler = delegate { OnMyTimer_Elapsed1(arg1,arg2);};//Yes I need to pass some args which are optional by the way
myTimer.Elapsed += handler;

//then do some processing (this may take 500ms to 2000 ms)

If(something == true){
    //remove previous elapsed event handler from myTimer object
    myTimer.Elapsed -= handler;//Notice -=

    //Then reattach different elapsed event handler
    myTimer.Elapsed += delegate { OnMyTimer_Elapsed2(arg1,arg2);}; 
}

Open in new window

0
 

Author Comment

by:ExcUsr2008
ID: 38285473
Ah ... I completely ignored the context/scope (i.e it needs a reference).

I just had to tweak the code as below since "Action" is not a valid type in this case:

System.Timers.ElapsedEventHandler handler = delegate { OnMyTimer_Elapsed1(arg1,arg2);};


Other than that it worked like a charm!. Many thanks to you!!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question