In my application, I am performing my file reading by another thread(other that GUI thread). There are two buttons that suspend and resume the Thread respectively.
I would use the Monitor mechanism for achieving pausing and resuming threads. The Monitor.Wait will cause the thread to wait for the Monitor.Pulse.
private bool _pause = false;
private object _threadLock = new object();
private void RunThread()
{
while (true)
{
if (_pause)
{
lock (_threadLock)
{
Monitor.Wait(_threadLock);
}
}
// Do work
}
}
private void PauseThread()
{
_pause = true;
}
private void ResumeThread()
{
_pause = false;
lock (_threadLock)
{
Monitor.Pulse(_threadLock);
}
}