Advertisement

05.29.2008 at 04:47PM PDT, ID: 23443308
[x]
Attachment Details

How do I stop a class from running

Asked by GavinS in Microsoft Visual C#.Net, C# Programming Language

Tags: Microsoft, Visual Studio 2005, C#

Hi All,
I have a class that creates a timer and raises an event event when the timer tick event occurs (See Code Snippet) I can create an instance of my timer class like this:

private TimerClass MyTimer;

private void button1_Click(object sender, EventArgs e)
{
    //Create timer with 1 second interval.
    MyTimer = new TimerClass(1000);
    ((TimerClass)MyTimer).TimerEvent += new TimerEventDelegate(Form1_TimerEvent);
}

and it happily ticks away. However I want to destroy the instance of my class and recreate it with a different constructor initialiser like this:

private void button2_Click(object sender, EventArgs e)
{
    //Create timer with 5 second interval
    MyTimer = new TimerClass(5000);
    ((TimerClass)MyTimer).TimerEvent += new TimerEventDelegate(Form1_TimerEvent);
}

I thought this would delete the first instance of MyTimer and a new instance would start. Instead I end up with 2 timers running. How do I delete the first instance of my TimerClass before creating a new one?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
public class TimerClass : Timer
    {
        public event TimerEventDelegate TimerEvent;
 
        Timer ClassTimer = new Timer();
 
        public TimerClass(int TimerInterval)
        {
            ClassTimer.Interval = TimerInterval;
            ClassTimer.Tick += new EventHandler(FirstTimer_Tick);
            ClassTimer.Enabled = true;
        }
 
        void FirstTimer_Tick(object sender, EventArgs e)
        {
            OnTimerEvent(new EventArgs());
        }
 
        public virtual void OnTimerEvent(EventArgs Args)
        {
            TimerEvent(this, DateTime.Now.ToLongTimeString() + " " + ClassTimer.Interval.ToString());
        }
    }
[+][-]05.29.2008 at 05:09PM PDT, ID: 21674356

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual C#.Net, C# Programming Language
Tags: Microsoft, Visual Studio 2005, C#
Sign Up Now!
Solution Provided By: rstomar
Participating Experts: 4
Solution Grade: A
 
 
[+][-]05.29.2008 at 05:21PM PDT, ID: 21674407

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 05:50PM PDT, ID: 21674498

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 06:34PM PDT, ID: 21674665

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.29.2008 at 07:07PM PDT, ID: 21674763

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.29.2008 at 08:54PM PDT, ID: 21675134

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.30.2008 at 12:18AM PDT, ID: 21675803

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.02.2008 at 03:48PM PDT, ID: 21696345

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628