C# Threading.Suspend 已过时,线程已被弃用?
在我的应用程序中,我正在通过另一个线程(GUI 线程除外)执行文件读取。有两个按钮分别用于暂停和恢复线程。
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< /p>
无论如何,我只运行一个线程(而不是GUI线程),那么我如何在这里应用同步或监视。
更新代码:
class ThreadClass
{
// This delegate enables asynchronous calls for setting the text property on a rich text box 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;
}
}
}
}
运行是 bool 值,一个线程控制它(最初是 true)
并启动一个线程,我在另一个类中创建这个类实例(也是这个启动线程)
In my application, I am performing my file reading by another thread(other than the GUI thread). There are two buttons that suspend and resume the Thread respectively.
private void BtnStopAutoUpd_Click(object sender, EventArgs e)
{
autoReadThread.Suspend();
}
private void BtnStartAutoUpd_Click(object sender, EventArgs e)
{
autoReadThread.Resume();
}
but I am facing this warning,
Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202
Anyhow, I run only a single thread (rather than a GUI thread), so How can I apply Synchronization here or monitor.
Update Code:
class ThreadClass
{
// This delegate enables asynchronous calls for setting the text property on a rich text box 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;
}
}
}
}
the run is bool value, a thread controls it(Initially it's true)
and to start a thread I am creating this class instance(this start thread also) in another class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意,所有操作都在线程“挂起”之前完成
你想要的
Note that all operations are completed before the thread is "suspended"
What you want
不推荐使用挂起和恢复的原因是因为无法保证线程将在执行的哪个点挂起。这是一件坏事。该问题已描述这里以及解决方案。
该解决方案应该涉及一个 WaitHandle(可能是 AutoResetEvent 或 ManualResetEvent),您可以使用它向 autoReadThread 发出停止/启动信号。
The reason Suspend and Resume are deprecated is because there are no guarantees at what point in the execution the thread will be suspended on. This is a bad thing. The issue is described here as well as a solution.
The solution should involved a WaitHandle (maybe AutoResetEvent or ManualResetEvent) which you can use to signal to your autoReadThread to stop/start.
我将使用监视器机制来实现暂停和恢复线程。 Monitor.Wait 将导致线程等待 Monitor.Pulse。
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.