打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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 我怎么只运行单线程(而不是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;
                //}
            }
        }       
    }

}
} run是bool值,一个线程控制它(最初是真的) 并启动线程我在其他类中创建此类实例(此启动线程)     
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#多线程解决界面卡死问题的完美解决方案 – 放肆雷特 | 胖子的技术博客
多线程中暂停与挂起方法suspend sleep join 区别及使用指导
Java 线程暂停与继续
Linux 进程中 Stop, Park, Freeze
sleep()和wait()有什么区别
C# 高定位高效率程序中 线程与线程池比较
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服