CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2010
    Posts
    2

    How to stop a process using a "Stop Button"

    Hi, I created a simple window with two buttons,
    the first one calls a function that lasts a long time,
    the second one sets the value of a variable "stop" to TRUE,
    that was initially set to FALSE.

    My intention is that, by pressing the first button it runs a long process,
    that controls if the stop variable is set to TRUE or FALSE for every loop,
    if the value is TRUE the function should return, so the process is stopped.

    Code:
        ...
    
        static BOOL stop = FALSE;   // My variable defined somewhere
        
        ...
    
        int longProcess ()   // My function
        {
            while(stop == FALSE) {
                 // do something
            }
            return 0;
        }
    
        ...
    
        switch (msg)
        {
    		case WM_CREATE:
    		{
                                    ...
    
    		    Button1 = CreateWindowEx(0, 
                                                 TEXT("BUTTON"),
                                                 TEXT("Start"),
                                                 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                                 100, 100, 100, 20,
                                                 hWnd,
    			                     (HMENU)BUTTON_START,
    			                     NULL,
                                                 NULL);
    
    		    Button2 = CreateWindowEx(0,
    				               TEXT("BUTTON"),
    					       TEXT("Stop"),
    					       WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
    					       200, 200, 100, 20,
    			                       hWnd,
    			                       (HMENU)BUTTON_STOP,
    			                       NULL,
    			                       NULL);
                                     ...
    		
    		}
    		break;
    
    		case WM_COMMAND:
            {
    			switch (LOWORD(wParam))
    			{
    
                                case BUTTON_START:
                                    longProcess();   // Starts the process
    			        break;
    
                                case BUTTON_STOP:
                                    stop = TRUE;  // Should stop the process
    			        break;
    
    			}
            }
    		break;
    
            ...
    
        }
    }
    The problem is that when I press the first button the process starts normally,
    but when I press the second button in order to stop the process nothing happens,
    I noticed that the stop variable is set to TRUE only after the end of the process.
    I thought that probably in a problem related to the message queue...

    What could be the best solution???

    Thanks!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to stop a process using a "Stop Button"

    Quote Originally Posted by mariop View Post
    What could be the best solution???

    Thanks!
    The best solution is to create a worker thread and move the code for long computation in this thread.
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2010
    Posts
    50

    Re: How to stop a process using a "Stop Button"

    Using a flag like that might be most appropriate in polling
    If you create a worker thread, then there are start-worker-thread, end-worker-thread and pause-worker-thread functions for you to do the long process

  4. #4
    Join Date
    Aug 2010
    Posts
    51

    Smile Re: How to stop a process using a "Stop Button"

    hi,

    I am mani
    pl give to me a best solution, that is .....
    when I press the first button the process starts normally,
    but when I press the second button in order to stop the process nothing happens,
    I noticed that the stop variable is set to TRUE only after the end of the process.

    thanks for advice



    regards,
    phe9oxis,
    http://www.guidebuddha.com

  5. #5
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: How to stop a process using a "Stop Button"

    There is a reason for two answers suggesting worker thread. Still possible but not recommended a local loop that would allow idle processing.
    I am not sure what do you mean by starting process; I am assuming that you mean long processing that takes place in the man execution thread, like a while or for loop.
    Doing this takes all CPU time for your application and does not allow for use UI to update. Also makes all controls inaccessible (appear to be frozen) since thread is not able to process any messages because of the loop.
    I bet you press the button but it does not go down. Windows delivers message to a que but they are retrieved after processing a loop is done.
    Hence, follow advice and do it in a worker thread.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

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