I am very inexperienced using multi-threading techniques, but here is what I have tried:
Thread thread = null;
for (int minute = 0; minute < 60; minute++)
{
Thread.Sleep(60000);
if (thread != null)
{
while (thread.ThreadState == ThreadState.Running) { }
}
thread = new Thread(delegate()
{
// Do stuff during the next minute whilst the main thread is sleeping.
});
thread.Start();
}
What I am trying to achieve here is to have a thread running and doing work whilst the main thread sleeps, but I am unsure why the above code doesn’t work. What happens is that following the first loop (after starting the thread) the ThreadState doesn’t seem to change from “Running”. I am also curious as to whether there is a more elegant way of doing this.
Anyone know the problem?
Thread.Join is a better way to wait for a thread to end.