I have a class like this:
class SomeClass
{
bool isWork;
Thread workThread;
public void Start()
{
isWork = true;
workThread = new Thread(new ThreadStart(DoWork));
workThread.SetApartmentState(ApartmentState.STA);
workThread.IsBackground = true;
workThread.Start();
}
public void Stop()
{
isWork = false;
workThread.Join();
}
}
void DoWork()
{
while (isWork)
{
// do work
Thread.Sleep(100);
}
}
When application starting it create instance of SomeClass and call it Start() method. Then I call Stop() method of that instance from UI thread and:
- isWork set to "false";
- main thread join to workThread;
- workThread check isWorked and finishet correctly;
- main thread freeze on workThread.Join().
As I understant joined thread should continue executing after workThread is over, but it's not true in my situation.
At moment when main thread call workThread.Join() the workThread is alive and exists in debug thread window. But when application freeze workThread not already exists.