DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Making external process sleep

  1. #1
    Join Date
    Jan 2008

    Making external process sleep


    Would it be possible with ShellExecute(hwnd, . . . .

    to get the handle of the application and send it sleep every few seconds. This is because the application is running at 100% of cpu power even though it doesn't need to. I also dont have the source to change it in there so this is my only way.

    Last edited by kpedersen; 01-12-2008 at 10:55 AM.

  2. #2
    Join Date
    Dec 2003
    no, it does not work that way. Shellexecute just calls a "dos" prompt command such as dir or cd or c:\something.exe just as you can do in the command prompt.

    You can open the task manager in windows, the on the app do go to process (right click) and right click again to set priority to lowest so every other application can interrupt it. Thats about the best you can do.

  3. #3
    Join Date
    Jan 2008
    yes, but it does let me get a handle... i was just hoping that there was a SendMessage(hwnd, WM_SLEEP, 0, 100) or something... lol

    it was a long shot anyway :p

    Last edited by kpedersen; 01-12-2008 at 10:55 AM.

  4. #4
    Join Date
    Nov 2003
    You need to use a different form of launching a child process, and then signal that child. However, if the process is in the middle of a tight loop, the signal may be delayed or even ignored. What you should really do is check why the process launched engages the CPU so much, and if you can modify its code you can insert a few Sleep() calls in the right places.
    Danny Kalev

  5. #5
    Join Date
    Jan 2008
    If I had the source, that is exactly what I would do... Basically Its a badly programmed 2D game I am helping my friends with. We have no source for it though so I can only manipulate it once its running. I am just seeing if there was a way to accomplish this without having to invest time adding the sleep using ASM

  6. #6
    Join Date
    Jan 2008
    Ah ok, I tried to set the processor priority in code so its invisible to the user. Something like this...

    #include <windows.h>
    #include <iostream>

    using namespace std;

    int main()
    HWND hWindow = 0;
    DWORD dwProcessID = 0;
    HWND hProcess = 0;

    hWindow = FindWindow(NULL, "Untitled - Notepad");
    cout << "hWindow: " << hWindow << "\r\n";


    dwProcessID = GetWindowThreadProcessId(hWindow, NULL);
    cout << "dwProcessID: " << dwProcessID << "\r\n";

    hProcess = (HWND)OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwProcessID);

    cout << "hProcess: " << hProcess << "\r\n";

    cout << "Error: " << GetLastError() << "\r\n";


    Its odd but I am not getting a handle on the process... Always returns 0 and GetLastError() returns 0 too.

    If anyone can help me with that then I can do SetPriorityClass(hProcess, BELOW_NORMAL_PRIORITY_CLASS);

    Its close.. I can feel it

    Last edited by kpedersen; 01-12-2008 at 02:13 PM.

  7. #7
    Join Date
    Mar 2007
    Bangalore, India
    Why don't you try creating a launcher for that application? In that way you will get a handle and full control. Maybe you could start that appn as a thread in your launcher. That way, you get control even during loops. Otherwise, if an while loop in which all code is written is causing that behavior, there's pretty much nothing that you can control by sending messages.

  8. #8
    Join Date
    Dec 2007
    #include <windows.h>
    #include <iostream>
    #include <cassert>
    using namespace std;
    int main()
      HWND window = FindWindowA( 0, "Untitled - Notepad" );
      assert(window) ;
      DWORD pid = 0 ;
      GetWindowThreadProcessId( window, &pid );
      assert(pid) ;
      HANDLE process = OpenProcess( PROCESS_QUERY_INFORMATION, 0,  pid );
      cout << "process: " << process << '\n';
      // ...
      CloseHandle(process) ;
    > GetLastError() returns 0 too.
    call GetLastError() immediately on return from the failed function.
    Last edited by vijayan; 01-14-2008 at 07:49 AM.

Similar Threads

  1. plz plz help anyone
    By amit.nikhil in forum VB Classic
    Replies: 1
    Last Post: 02-07-2007, 06:29 PM
  2. Replies: 5
    Last Post: 01-17-2007, 02:37 PM
  3. Link error
    By Shar in forum C++
    Replies: 5
    Last Post: 12-19-2006, 07:07 AM
  4. Replies: 1
    Last Post: 04-21-2000, 11:43 AM
  5. Replies: 0
    Last Post: 04-12-2000, 12:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center