C#Threading.Suspend已过时,线程已被弃用? [英] C# Threading.Suspend in Obsolete, thread has been deprecated?

查看:1245
本文介绍了C#Threading.Suspend已过时,线程已被弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在通过另一个线程(该GUI线程除外)执行文件读取.有两个按钮分别挂起和恢复线程.

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.

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已被弃用.请使用System.Threading中的其他类(例如Monitor,Mutex,Event和Semaphore)来同步线程或保护资源. http://go.microsoft.com/fwlink/?linkid=14202

任何我都只运行单个线程(而不是GUI线程)的方法,因此如何在此处应用同步或监视.

Any how I run only single thread (rather than GUI thread), so How can I apply Synchronization here, or monitor.

更新代码:

 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;
                //}
            }
        }       
    }

}

}

run是bool值,一个线程控制它(最初是true)

run is bool value, a thread controls it(Initially its true)

要启动线程,我要在其他类中创建这个类实例(也就是这个启动线程)

and to start thread I am creating this class instance(this start thread also) in other class

推荐答案

 //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();

请注意,所有操作都在线程被暂停"之前完成

Note that all operations are completed before the thread is "suspended"

您想要的

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;
}

这篇关于C#Threading.Suspend已过时,线程已被弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆