0

I have a TimerMethod(), that calls itself at five-second intervals. So far all is good, the timer loops as expected. Inside the timer, I put a method - SomeThreadMethod(). If I do not start a thread inside that SomeThreadMethod, all is good, the timer continues looping. However, if I start a thread, the timer stops looping. What is wrong with that code and how can I use a thread inside a looping timer?

    public void TimerMethod()
    {
        Timer timer = new Timer((obj) =>
        {
            // this point in the code is always reached
            System.Diagnostics.Debug.WriteLine("before function call");

            SomeThreadMethod();
             
            // this point is never reached, if there is a nested Thread
            // inside SomeThreadMethod()
            System.Diagnostics.Debug.WriteLine("after function call");


            TimerMethod();
            timer.Dispose();
        },
        null, 5000, Timeout.Infinite);
    }


    public void SomeThreadMethod()
    {
        // if I use thread here, then the hosting 
        // TimerMethod stops looping. Why???
        // If I do not use a thread here, then
        // the timer loops normally
        Thread someThread = new Thread(() =>
            {
                // do something inside thread
            });

        someThread .Start();
        someThread .Join();                                                
    }
user2217057
  • 237
  • 3
  • 17
  • 2
    If you join the thread right after starting it then you do not need a thread. – Fildor Sep 16 '21 at 08:49
  • 1
    Why do you cancel the timer and start another one from inside the timer's tick handler? – canton7 Sep 16 '21 at 08:50
  • 2
    The small amount of code you have is clearly wrong for a bunch of reasons, I think this might be easier if you explain what you are trying to achieve. – TheGeneral Sep 16 '21 at 08:51
  • This is a simplified version of the original code. There are multiple threads starting inside SomeThreadMethod – user2217057 Sep 16 '21 at 08:53
  • Then there are two issues: The timer is not used as intended, here and you probably are using too many threads. So, if you could post a [mcve] along with a description of what exactly your goal is, that would be great. – Fildor Sep 16 '21 at 08:55
  • Multithreading is not my specialty, sorry, the goal is to call a method inside a periodically looping timer. That method has multiple threads inside it. – user2217057 Sep 16 '21 at 08:56
  • No, that's what you are doing and which obviously doesn't work as expected. But what _problem_ are you trying to solve? – Fildor Sep 16 '21 at 09:04
  • Thanks for your comments: "If I do not start a thread inside that SomeThreadMethod, all is good, the parent timer continues looping. However, if I start a thread, the timer stops looping." Why does starting thread inside a child method break parent timer? – user2217057 Sep 16 '21 at 09:12

1 Answers1

0

I don't know what your plan is. Here is a working Version of your thread starting Timer. Here the Thread does not break the Timer.

using System;
using System.Threading;
using System.Timers;

namespace TestTimerThread
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press key to end");
            System.Timers.Timer timer = new System.Timers.Timer()
            {
                Interval = 5000
            };
            timer.Elapsed += OnTimerElapsed;
            timer.Start();

            Console.ReadKey();
            timer.Stop();
        }

        private static void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            Console.WriteLine("Elapsed before function call");
            RunThread();
            Console.WriteLine("Elapsed after function call");
        }

        private static void RunThread()
        {
            Thread thread = new Thread(() =>
            {
                Console.WriteLine("in Thread");
            });
            thread.Start();
        }
    }
}
J. S. Garcia
  • 366
  • 1
  • 9