CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Stopping thread

  1. #1
    Join Date
    May 1999
    Location
    Moscow, Russia
    Posts
    35

    Stopping thread

    What is the proper way to stop the thread ? Thread.stop() is deprecated and furthermore doesn't seem to work corectly. Help please !


  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: Stopping thread

    What is the safe way to stop a thread depends on the logic inside the thread. There is a detailed article in JDK1.2 help. It is quite explanative and a bit confusing also (if u r not going to develop a "heavy" thread).

    If u want to create a simple thread which should stop safely when u say 'stop', it is recommended that u don't use 'Thread.stop()' method. So what should u do?

    1. In most cases the thread is doing some repeatative things in its 'run' method. Obviously the 'run' method will have a "loop". Here it is very simple to implement 'stop' functionality as shown.


    public class TestClass extends Thread
    {
    private mbRUN = true;

    public TestClass()
    {
    // Do ur normal coding (initialization etc) here.
    }

    public void stop()
    {
    mbRUN = false;
    // The difference here is, u r "requesting" the thread to stop so that it will "stop"
    // when it is in a safe state
    }

    public void run()
    {
    mbRUN = true; // Use this statement if u want to run the thread after
    // it was stopped previously.

    // There may be any loop, in place of 'while' ...
    while( (...your conditions...) && mbRUN )
    {
    // here u will have the repeatative things, which u want to run in
    // this thread.
    }
    // here all ur cleaning code will be there
    }
    }




    As shown in the above example, u don't have to make extensive modifications to ur logic (only code added is for setting and checking 'mbRUN' value). This way u know that whenever 'stop' method of above class is called, it doesn't stop immediately. It will complete its iteration, and when it will check 'mbRUN', it will know that it should stop and come out safely.

    I don't know if u r convinced with this answer, but if u want more details tell me. I won't say I know everything, but I will surely help as far as I know.

    - UnicMan
    http://members.tripod.com/unicman

  3. #3
    Join Date
    Apr 1999
    Location
    romania
    Posts
    43

    Re: Stopping thread

    the method thread.stop() isn't deprecated.
    u may use
    if(threat != NULL)<br

  4. #4
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: Stopping thread

    I think u r referring to JDK1.1.
    Its not deprecated in JDK1.1, but it is depricated in JDK1.2.
    Please confirm.

    - UnicMan
    http://members.tripod.com/unicman

  5. #5
    Join Date
    May 1999
    Location
    Moscow, Russia
    Posts
    35

    Re: Stopping thread

    Thanx for your response. But the way you suggest cannot really help me. In you example the "loop" doesn't break immediately but first comletes its current iteration. In my program such an iteration may take a lot of time (it waits for a response from remote host). And I wanted it to stop immediately. Can you help me ?


  6. #6
    Join Date
    Apr 1999
    Location
    romania
    Posts
    43

    Re: Stopping thread

    I worked only with Visual J++ 1.1



  7. #7
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: Stopping thread

    Well if your iteration does many things, what u can do is keep a simple condition in your loop...


    while ( ... ) // the same loop I had written
    {
    .... // do ur stuff
    if( !mbRUN ) break;
    .... // do ur stuff
    if( !mbRUN ) break;
    .... // do ur stuff
    }




    I know this is tedious, but it atleast ensures that ur thread and objects r in safe state.

    And talking about socket, u can keep 'Socket' as member variable in your thread. And then in 'stop' method close the connection. When u close connection of a 'Socket' it will break off and u can then come out of 'run' method safely.

    - UnicMan
    http://members.tripod.com/unicman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured