线程方法出现问题(挂起和恢复) [英] probleme with thread methodes (suspend & resume)

查看:147
本文介绍了线程方法出现问题(挂起和恢复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI ...
我在暂停和恢复方法中遇到线程问题,请在此处 [ ^ ] ...感谢您

这是我在其中创建线程的btnstart ...

HI...
I have a problem with thread in suspend and resume methodes, j''ai toujours ce message here[^] ...thank you

this is the btnstart where i create the thread ...

private void btnStart_Click(object sender, EventArgs e)
       {

           tbPort.ReadOnly = true;
           tbPath.ReadOnly = true;
           //la creation d'une list des socket
           int port = 0;
           port = Convert.ToInt16(tbPort.Text);
           string path = tbPath.Text;
           //alSockets = new ArrayList();
           Listener lst = new Listener(port, path, this.lbconnect);
           //creation d'une thread appeler thdlistener
           thdListener = new Thread(new ThreadStart(lst.listenerThread));
           //le debut de travail de thread...on utilisant la methode start
           thdListener.IsBackground = true;
           thdListener.Start();

           ////////////////////////////////
           btnStart.Enabled = false;
           Resume.Enabled = false;
           Suspend.Enabled = true;
           Stop.Enabled = true;
       }



这是我暂停线程的按钮...



and this is the button where i suspend the thread ...

private void Suspend_Click(object sender, EventArgs e)
        {
           thdListener.Suspend();
           Resume.Enabled = true;
           Suspend.Enabled = false;
           lbConnections.Items.Add("Server Suspended");
        }



狂热地恢复按钮...



fanally the resume button ...

private void Resume_Click(object sender, EventArgs e)
        {
            thdListener.Resume();
            Resume.Enabled = false;
            Suspend.Enabled = true;
            lbConnections.Items.Add("Server Resumed");
        }



我希望您能找到问题...



i hope that you can find the problem...

推荐答案

我不知道侦听器是什么类型.但是,如果它是 TcpListener [
I do not know what type of class Listener is. But if it is a TcpListener[^] class, you can do:

private void Suspend_Click(object sender, EventArgs e)
        {
           Listener.Stop(); // Closes the listener.
           Resume.Enabled = true;
           Suspend.Enabled = false;
           lbConnections.Items.Add("Server Suspended");
        }







private void Resume_Click(object sender, EventArgs e)
        {
            Listener.Start(); // Starts listening for incoming connection requests.
            Resume.Enabled = false;
            Suspend.Enabled = true;
            lbConnections.Items.Add("Server Resumed");
        }



---


Thread.Suspend()和.Resume()不是暂停和恢复线程操作的正确方法,您无法知道线程的运行距离. (点击过时")

使用其他方法可以实现您的目标.查看
线程同步 [



---


Thread.Suspend() and .Resume() is not the right way to halt and resume operation on a thread, you have no way to know how far you thread has come. (Hit "Obsolete")

Use other methods to reach your goal. Look at Thread Synchronization[^]


请勿使用 已弃用 System.Threading.Thread.Suspend!阅读此方法的帮助页面以了解原因:
http://msdn.microsoft.com/en-us/library/system. threading.thread.suspend.aspx [ ^ ].

使用System.Threading.EventWaitHandle的实例.您的线程应使用
EventWaitHandle.WaitOne,并且UI线程将在同一实例EventWaitHandle.Set上进行调用(以唤醒正在等待的线程,或者如果线程在调用WaitOne时未处于等待状态,则允许线程继续执行)保持线程处于等待状态).

通常,您不需要在任何给定的点冻结线程,无论如何这都不是准确的(就像您使用Suspend一样).同时,该线程将被另一个线程中调用的Thread.Abort唤醒.
—SA
Do not use deprecated System.Threading.Thread.Suspend! Read the help page in this method to understand why:
http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend.aspx[^].

Use an instance of System.Threading.EventWaitHandle. Your thread should use
EventWaitHandle.WaitOne, and UI thread will call on the same instance EventWaitHandle.Set (to wake up the waiting thread or allow thread to proceed if it was not in the waiting state at the call to WaitOne) or EventWaitHandle.Reset (to keep thread in a wait state).

Usually, you do not need to freeze the thread in any given point, which could not be accurate anyway (as if you used Suspend). At the same time, the thread will be awaken by Thread.Abort called in another thread.

—SA


这篇关于线程方法出现问题(挂起和恢复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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