Thread: suspend execution of a program

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    suspend execution of a program

    hi

    I am searching for a way to suspend the execution of a C/C++ program.. I know there is the Sleep function but that only stops the program for a specified number of seconds...

    what i need is a function that suspends the flow of control. then i need to reactivate the flow through some event.
    take for example message boxes.. in any language when you pop up a message box the flow of control freezes until the user clicks some button on the message box and then the program resumes.. i need to do something similar

    is that possible? or do i have to think about threads and mutexes?because i'm trying to avoid that..

    if you need more clarification please ask me.

    thank you

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes... what are you trying to do?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    well actually i'm trying to create my own message boxes..
    and i need a way to stop execution of my program after the message box pops up and wait for user events. then resume the execution after the user clicks one of the message box buttons..

    i'm using win32 by the way..

    in VB i know that there is a stop function that does what i want.. i'm sure there must be a way in C/C++ also..

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Message boxes have a modal attribute. I have no idea how this is implemented in C++ but it certainly is no much different from what VB does (assuming you are using the windows API to call your Message Box)

    If you used stop() with your messages boxes in VB you were already doing it wrong.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    The most common way to wait for whatever is the following:
    Code:
    bool b_wait = true;
    
    while(b_wait){
    	// ...
    }
    Inside that loop you check if the user pushed your button, or whatever quota that needs to be fulfilled.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    i already tried a while loop..
    the program wont catch user events..it will get stuck in that infinite loop..

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Write a message loop. That's exactly what modal dialog boxes do: they have their own message loop and only leave it when the dialog is dismissed.

    Code:
    while(GetMessage(&msg, ...) > 0) {
      if(!IsDialogMessage(hDlg, ...)) {
        ...
      }
      if(!dialogBoxActive) {
        break;
      }
    }
    There are some complexities involved in writing your own modal message loop, though. See this blog for some of the pitfalls:
    http://blogs.msdn.com/oldnewthing/
    Search for "modality". Consider especially this post and the follow-ups:
    http://blogs.msdn.com/oldnewthing/ar...18/376080.aspx
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    As Osaou said:

    Code:
    bool StayInLoop = true;
    
    while ( StayInLoop )
    {
    	// Do the message box thing
    	if ( ReturnFromMB Indicates you break ) StayInLoop = false;
    }
    >> the program wont catch user events..it will get stuck in that infinite loop..
    I don't see how

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by twomers
    I don't see how
    The message box can't do anything without a message loop, ergo it won't ever go away or return or anything.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. execution of a C program
    By kris.c in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 11:14 AM
  3. invoke the execution of one c program from another
    By kris.c in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-08-2006, 11:10 PM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM