Pause/idle thing

This is a discussion on Pause/idle thing within the Windows Programming forums, part of the Platform Specific Boards category; As some of you have noticed, I'm new to winprogramming My aim is to create a program that would send ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    22

    Pause/idle thing

    As some of you have noticed, I'm new to winprogramming
    My aim is to create a program that would send messages to some other program, without user interference, with specified delays.
    Currently my program grabs nearly all CPU while computing, hardly letting other programs do anything.I need to slow him down so that other progs could keep up.

    Another thing I need to do is to make him go idle for say, 1 minute, then continue doing things. Again, I have no clue what possibilities there are. I've tried searching of course but it's like shooting in the dark :s.

    Thanks.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    There is no reason why you should be using a busy wait, (a loop), as a timing device in Win32 programming. Use SetTimer() and process the WM_TIMER messages as they arrive. There is nothing then stopping other tasks running, or your own task doing other things.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    To give more time slices to other processes use the Sleep( ) function and pass the number of milliseconds you want to give.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Thank you both, these 2 things are exactly what I was looking for.

    SetTimer would probably be best solution but in my case inserting some Sleep(fewmillisecs) here and there will probably be enough.

    To adrian: I didn't even consider using busy loop for timing, I'm newbie but I have some sense :P

    Still stuck due to my other thread problem :\

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> I'm newbie but I have some sense :P

    Well, whatever you were doing, it was hogging the CPU, so not that much sense eh?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Originally posted by adrianxw
    >>> I'm newbie but I have some sense :P

    Well, whatever you were doing, it was hogging the CPU, so not that much sense eh?
    That's why I made this thread. There's a difference between lacking knowledge and being unreasonable.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by adrianxw
    >>> I'm newbie but I have some sense :P

    Well, whatever you were doing, it was hogging the CPU, so not that much sense eh?
    At least he had the sense to ask about how to fix it.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    But not enough to recognise the wink on the end of the line!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Excuse me my ignorance but... I still haven't figured out how to do it. It's actually about how to build up the program.

    I want my program to control some physical process. It needs to supervise it and send control commands to it if required. Something like controlling a robot for example (just an example).

    Now... I wrote a piece of complex code to follow algorithms and constantly read process measurements to make decisions. Everything is fine except that it totally owns the CPU and it can't accept messages (even such as WM_QUIT ).
    It's obvious why it's happening - I wrote the whole thing to my windows WM_KEYDOWN handler, which means it starts when I press a key. And never stops, because it's still "handling" the WM_KEYDOWN message, which means no other messages can be processed. Obviously not very clever solution. Not gonna do it again, honest :P.

    Solution 2 would be to write everything to main(the startup) function and inserting PeekMessages everywhere. Perhaps some Sleep() somewhere or WaitMessage(with set up timer) to force stop and spare some time to other progs. But that would mean a lot of redundant code, I would have to insert those calls in every function and its hard to keep it all balanced. Besides, losing few milliseconds in a wrong place can be a very bad thing.


    Both solutions are very... I'm kinda reinventing bycicle here, could someone just point out how it's done in a more reasonable manner? I haven't programmed in multitasking enviroments before, so I just need to acquire some new concepts.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    When you want to do things like this, what you need is, (probably), multithreading.

    I say probably, because there are some fiddles you can do with sertain types of processor hogging tasks. One thing that you may be able to do is a "state machine" type solution. Here, you start your process, and let it run to a certain point when you store where you are then return control to the system, if there is nothing for the system to do, you restart from the last hold point. You may well be able to do this in your main with periodic Peeks as you suggest.

    To multithread it, what you do is when your trigger event occursm rather than running the process in the windows loop, (ALWAYS a bad idea), you spin a worker thread. Your Windows loop is then free to process messages as before. Your thread can communicate it's status to the main windows loop by using PostMessage(), (in this case better than SendMessage()).

    Use beginthread() to start a worker thread, (beginthreadex() if you need more control - but I doubt that). Avoid CreateThread if you are using routines in the C standard library as some of them are, or at least were not thread safe as they use local storage which is destroyed/corrupted by multiple threads, it can also leak memory.

    Look up beginthread() and see if you can get a simple app to work. Be careful to have the worker thread use only it's own data. If you r main thread and worker thread are using shared data, you will need to start adding synchronisation to prevent collisions.

    I had a great big tutorial on multithreading on my old web site but sadly, it is not converted to my new site yet. If I get a minute, I'll post a very simple program, (I have to dig it out of some archives which I do not have online).
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Hah! I found a little example on my hard disk. This is a program I wrote to prove that inapparopriate multithreading can actually make a program run slower. What it does is runs three functions one after the other, then spins three threads running one function in each in parallel. Have a look at it and see how you get on.
    Attached Files Attached Files
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Heh, that's an interesting experiment you got there. Didn't get any good ideas from there tho. I need some Windows programming concept but I'm not even sure what I'm looking after. I don't want to create artificial pauses etc., I'm sure there is an elegant solution built in to Windows mechanism to my problem but I'm just too new figure it out.

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You put your processor hog in a function, and in the WM_KEYDOWN, call beginthread(). It is just as applicable to a GUI program. I use it all the time. Hell, it don't get more elegant than multithreading.

    Multithreading is a key technology within Windows GUI systems. The main thread handles the user interface, threads do all the work in parallel.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    So "threads" is the magic word. I suspected that they could be key to solution but they looked too scary(read: complicated) to get deeply into right away .
    You see, I'm learning programming Windows for the first week.
    But with you guys helping, I have learned plenty

    Thanks for the tip, I'll look into them.

    *edit* Argh, missed your "big" post and started reading from your post with example .cpp . Thanks, you enlightened me a lot.

    Didn't see any beginthread()... I guess I'll just use CreateThread().
    I don't envy my opsys for what is going to happen to him now...
    Last edited by freedik; 08-22-2003 at 11:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nifty little thing i made
    By DavidP in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2003, 11:43 PM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 01:43 PM
  3. most challenging thing to program
    By volk in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 03-28-2003, 03:56 PM
  4. newbie needs help comprehending simple thing
    By A helpless one in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2002, 09:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21