Reading people the lines I think you need to just build extra logic into your code so it knows when to stop and when to continue rather than pausing and resuming the thread.
public class Navigator
{
private bool isRunning;
private Thread thread;
private ManualResetEvent resetEvent = new ManualResetEvent(true);
public List<string> Urls { get; set; }
public Navigator()
{
this.isRunning = false;
this.Urls = new List<string>();
}
public void Start()
{
if (this.isRunning)
{
return;
}
this.isRunning = true;
thread = new Thread(Process);
thread.Start();
}
private void Process()
{
foreach (string url in this.Urls)
{
if (!this.isRunning)
{
return;
}
Debug.WriteLine(string.Format("Processing {0}", url));
Thread.Sleep(3000);
resetEvent.WaitOne();
}
}
public void Pause()
{
this.resetEvent.Reset();
Debug.WriteLine("Paused");
}
public void Resume()
{
this.resetEvent.Set();
Debug.WriteLine("Resumed");
}
public void Stop()
{
Debug.WriteLine("Stopping...");
this.isRunning = false;
this.resetEvent.Set();
this.thread.Join();
Debug.WriteLine("Stopped");
}
}
private Navigator nav;
public Form1()
{
InitializeComponent();
}
private void startBtn_Click(object sender, EventArgs e)
{
this.nav = new Navigator();
nav.Urls.Add("http://www.url1.com");
nav.Urls.Add("http://www.url2.com");
nav.Urls.Add("http://www.url3.com");
nav.Urls.Add("http://www.url4.com");
nav.Urls.Add("http://www.url5.com");
nav.Urls.Add("http://www.url6.com");
nav.Urls.Add("http://www.url7.com");
nav.Urls.Add("http://www.url8.com");
nav.Urls.Add("http://www.url9.com");
nav.Urls.Add("http://www.url10.com");
nav.Start();
}
private void pauseBtn_Click(object sender, EventArgs e)
{
if (this.nav == null)
{
return;
}
nav.Pause();
}
private void resumeBtn_Click(object sender, EventArgs e)
{
if (this.nav == null)
{
return;
}
nav.Resume();
}
private void stopBtn_Click(object sender, EventArgs e)
{
StopProcess();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StopProcess();
}
private void StopProcess()
{
if (this.nav == null)
{
return;
}
nav.Stop();
}