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();
}