2010-12-22 210 views
9

在我的應用程序中,我正在通過另一個線程(其他GUI線程)執行我的文件讀取。有兩個按鈕分別暫停和恢復線程。C#Threading.Suspend已廢棄,線程已被棄用?

private void BtnStopAutoUpd_Click(object sender, EventArgs e) 
     { 
      autoReadThread.Suspend(); 
     } 

private void BtnStartAutoUpd_Click(object sender, EventArgs e) 
     { 
      autoReadThread.Resume(); 
     } 

,但我面對這樣的警告,

Thread.Suspend已被棄用。請使用System.Threading中的其他類(如Monitor,Mutex,Event和Semaphore)來同步線程或保護資源。 http://go.microsoft.com/fwlink/?linkid=14202

任何如何運行只有單線程(而不是GUI線程),所以我如何在這裏應用同步或監視。

更新代碼:

class ThreadClass 
{ 

    // This delegate enables asynchronous calls for setting the text property on a richTextBox control. 
    delegate void UpdateTextCallback(object text); 

    // create thread that perform actual task 
    public Thread autoReadThread = null; 

    public ManualResetEvent _event = new ManualResetEvent(true); 

    // a new reference to rich text box 
    System.Windows.Forms.RichTextBox Textbox = null; 

    private volatile bool _run; 

    public bool Run 
    { 
     get { return _run; } 
     set { _run = value; } 
    } 

    public ThreadClass(string name, System.Windows.Forms.RichTextBox r1) 
    { 
     Textbox = r1; 
     Run = true; 
     this.autoReadThread = new Thread(new ParameterizedThreadStart(UpdateText)); 
     this.autoReadThread.Start(name); 
    } 

    private void UpdateText(object fileName) 
    { 

     //while (true) 
     //{ 
     // _event.WaitOne(); 
      while (Run) 
      { 

       if (Textbox.InvokeRequired) 
       { 
        UpdateTextCallback back = new UpdateTextCallback(UpdateText); 
        Textbox.BeginInvoke(back, new object[] { fileName }); 
        Thread.Sleep(1000); 
       } 

       else 
       { 
        string fileToUpdate = (string)fileName; 
        using (StreamReader readerStream = new StreamReader(fileToUpdate)) 
        { 
         Textbox.Text = readerStream.ReadToEnd(); 
        } 
        break; 
       //} 
      } 
     }  
    } 

} 

}

運行是布爾值,線程控制它(最初真的)

,並啓動線程我創建這個類的實例(此啓動螺紋也)其他類

+0

您更新不顯示:a)您的線程是如何啓動的,b)完整的線程方法。 – jgauffin 2010-12-22 13:35:30

+0

InvokeRequired始終爲真。你的線程沒有做任何有用的事情,一切都在UI線程上運行。 – 2010-12-22 14:29:06

回答

8
//true makes the thread start as "running", false makes it wait on _event.Set() 
    ManualResetEvent _event = new ManualResetEvent(true); 
    Thread _thread = new Thread(ThreadFunc); 

    public void ThreadFunc(object state) 
    { 
     while (true) 
     { 
      _event.Wait(); 

      //do operations here 
     } 
    } 


    _thread.Start(); 

    // to suspend thread. 
    _event.Reset(); 

    //to resume thread 
    _event.Set(); 

請注意,所有的操作都完成了前線程被「暫停」

你想

private void ThreadFunc(object fileName) 
{ 
    string fileToUpdate = (string)fileName; 
    while (Run) 
    { 
     _event.WaitOne(); 

     string data; 
     using (StreamReader readerStream = new StreamReader(fileToUpdate)) 
     { 
      data = readerStream.ReadToEnd(); 
     } 

     if (Textbox.InvokeRequired) 
     { 
      UpdateTextCallback back = new UpdateTextCallback(UpdateText); 
      Textbox.BeginInvoke(back, new object[] { data }); 
     } 

       Thread.Sleep(1000); 
    }  
} 


private void UpdateText(string data) 
{ 
    Textbox.Text = data; 
} 
5

原因掛起和恢復已被取消是因爲沒有保證在執行過程中什麼時候該線程將被暫停什麼。這是一件壞事。該問題描述爲here以及解決方案。

的解決方案應該涉及到的WaitHandle(可能的AutoResetEvent或ManualResetEvent的),你可以用它來通知您autoReadThread停止/啓動。

3

我會用實現暫停和恢復線程的監控機制。 Monitor.Wait將導致線程等待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); 
    } 
}