1
我有以下代碼。我試圖讓按鈕主要形式,它可以暫停,繼續和停止後臺線程下載器上(專用線程線程)暫停後臺線程()
Form1.cs的
private AutoResetEvent waitHandle = new AutoResetEvent(true);
private Thread thread;
private void ThreadJob()
{
Downloader download = new Downloader();
download.runDownloader();
}
// THREADS button1 is "Download now"-button
private void button1_Click(object sender, EventArgs e)
{
ThreadStart job = new ThreadStart(ThreadJob);
thread = new Thread(job);
thread.IsBackground = true;
thread.Start();
}
這個代碼在Windows上運行跑形成。我對所有這些動作的按鈕(暫停,繼續,停止)
暫停和繼續按鈕上的形式
private void btnPause_Click(object sender, EventArgs e)
{
waitHandle.WaitOne(); // Need to pause the background thread
}
private void btnContinue_Click(object sender, EventArgs e)
{
waitHandle.Set(); // Need to continue the background thread
}
問題是按暫停鍵將凍結的主要形式不是後臺線程代碼。
,「這是runDownloader()必須能夠暫停。」真的幫助:)謝謝。 –