Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
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 ...

C#
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 ...

C#
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 ...

C#
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...
Posted
Updated 24-Apr-11 6:51am
v3
Comments
walterhevedeich 24-Apr-11 9:50am    
You may want to post your code so others can further see the problem.
Kim Togo 24-Apr-11 10:34am    
Please provide us with more info, code etc.
Oshtri Deka 24-Apr-11 11:27am    
Warnings' messages have already answered to your question, you even have link to msdn. Suspend and Resume methods are obsolete.

I do not know what type of class Listener is. But if it is a TcpListener[^] class, you can do:

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



C#
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() 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[^]
 
Share this answer
 
v2
Comments
guendouz bachir 24-Apr-11 10:59am    
see the code in down plz
Kim Togo 24-Apr-11 15:06pm    
See my updated answer.
Sergey Alexandrovich Kryukov 24-Apr-11 13:02pm    
Please see explanation and the alternative in my answer.
--SA
guendouz bachir 24-Apr-11 16:39pm    
Mr Kim Togo you are wrong the tcplistener class don't contain start and stop methodes...
Kim Togo 25-Apr-11 2:09am    
What documentation are you looking at? IF your "Listener(...)" is a class of TcpListener, then yes TcpListener.Start() and TcpListener.Stop() both exists.

Please see MSDN:

TcpListener.Start()
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.start.aspx

TcpListener.Stop()
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.stop.aspx
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
 
Share this answer
 
v3
Comments
guendouz bachir 24-Apr-11 13:20pm    
I don't understand ? "_"
Sergey Alexandrovich Kryukov 24-Apr-11 14:56pm    
There is no "_" in my answer. What do you want to understand? The right solution for your application is quite clear...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900