How do I get to timers that were started by a C #

0

Hello, I'm trying to understand a situation but until then I could not solve it. Imagine that we have a class X that has a timer that is started by the constructor. Now imagine that this same class is instantiated inside a thread.

By doing some tests in my code, I realized that if I have a dispose in the thread, the timer that is running in the class will continue to run. I already tried to create a list and try to stop the timer of the objects but it still did not work.

An example of the problem in a C # console solution -

public class Phone
    {
        private static System.Timers.Timer aTimer;

        private int ID;

        public Phone(int ID)
        {
            this.ID = ID;
        }

        public void Start()
        {
            aTimer = new System.Timers.Timer(2000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        public void Stop()
        {
            aTimer.Stop();
            aTimer.Dispose();
        }

        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            Console.WriteLine("Radio ID : " + ID);
            Console.Write(" Launch thread: {0}", Thread.CurrentThread.ManagedThreadId);
        }
    }

    class Program
    {
        static void Main()
        {
            List<Task> list = new List<Task>();

            for (int i = 0; i < 10; i++)
            {
                Phone A = new Phone(i);


                list.Add(Task.Factory.StartNew(() => {

                    A.Start();

                }));


            }

            Console.ReadKey();
            Console.WriteLine(" STOPPING THREAD");

            for (int i = 0; i < 10; i++)
            {
                list[i].Dispose();
            }

            Console.ReadKey();
        }

    }
}
    
asked by anonymous 20.09.2017 / 02:11

1 answer

0

You have instantiated the object inside the main thread, not in the thread you are giving yourself ... you just created another thread to call the start ... enfin ...

In this situation you could put your Phone class to implement the IDisposable interface and then implement the Dispose method of your class, plus if the Phone should work asynchronously, you can put the Task inside it, which prevents implementation the timer. I made a sample code:

  

Class Phone

public class Phone : IDisposable
{
    private int ID;
    private Task t;
    private bool running = false;
    public Phone(int ID)
    {
        this.ID = ID;
    }

    public void Start()
    {
        running = true;
        Console.WriteLine("Radio Start : " + ID);
         t = Task.Factory.StartNew(() =>
            {
                while (running)
                {
                    Console.WriteLine("Radio ID: {0} Running in Thread {1}", ID, Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(1000);
                }
            });
    }

    public void Stop()
    {
        if (running)
        {
            running = false;
            Console.WriteLine("Radio Stopping : " + ID);
            while (t.Status == TaskStatus.Running)
            {
                Console.WriteLine("Parando thread...: " + Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(500);
            }
            Console.WriteLine("Radio Stopped : " + ID);
            t.Dispose();
        }
        else
        {
            Console.WriteLine("Radio not Running : " + ID);
        }
    }

    public void Dispose()
    {
        this.Stop();
        Console.WriteLine("Radio Disposed : " + ID);
    }
}
  

Execution:

class Program
{
    static void Main(string[] args)
    {
        List<Phone> phones = new List<Phone>();

        for (int i = 0; i < 10; i++)
        {
            Phone obj = new Phone(i);
            phones.Add(obj);
            obj.Start();
        }

        Console.ReadKey();
        Console.WriteLine("STOPPING THREAD");

        foreach (Phone p in phones)
        {
            p.Dispose();
        }

        Console.WriteLine("All Threads stopped");
        Console.ReadKey();

    }
}
    
20.09.2017 / 03:52