Results 1 to 8 of 8

Thread: Optimization code

  1. #1
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default Optimization code

    I wrote this program for work. It's a custom application, but basically I check what state the application is in, if it's in state 1 then it minimizes to the taskbar, if it's in state2 then it goes to it's default window state.

    The program itself works flawlessly, but I notice that it laggs the hell out of my system. I'm thinking it's because of all the loops. When I check the taskmgr I see my CPU usage go up to 50% from like 0-10%. That's a HUGE increase for such a small application, I would like to hear some feedback from users to see what I could potentially do to make it run a bit better.

    Code:
    // blah.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <math.h>
    #include <windows.h>
    using namespace std;
    /*
    bool minimized = false;
    bool maximized = true;
    */
    bool msgHide = false;
    bool msgShow = false;
    
    int _tmain( HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        //Afx:00400000:b:00010011:00000000:00000000 --> This is VERY odd, but it's the handle to the ShoreWare Agent Tool Bar
        HWND hWnd = FindWindow("Afx:00400000:b:00010011:00000000:00000000", NULL );
    
        /*Local Variables*/
        WINDOWPLACEMENT wd;
        UINT nCurShow;
    
        //We do a while loop (this allows the program to run in the background)
        while(1)
        {
            // Stealth mode so we don't see the command prompt window
            /* Hmm screws up my code argh :S (will look into later)
            HWND stealth;
            AllocConsole();
            stealth=FindWindowA("ConsoleWindowClass",NULL);
            ShowWindow(stealth,0);
            */
            // jajajaja
    
            //If you hit Esc, it will close the program
            if((GetAsyncKeyState(VK_ESCAPE)&1))
                return 0;
    
            //Check if the window handle exists
            if(hWnd)
            {
                /*Local Variables declared to get a Process Handle ont he Window Object*/
                DWORD ProcessID; 
                GetWindowThreadProcessId( hWnd, &ProcessID ); 
                HANDLE Process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID ); 
           
                //Check if the process exists
                if(Process)
                {
                    /*More local variables declared. These variables will hold the read information from the memory address*/
                    BYTE data;
                    DWORD datasize = sizeof(data);
                    //Read the data from memory
                    ReadProcessMemory(Process,(void *)0x005556FC, &data, datasize, 0); 
                    
                    /*
                        We can check to see what state the agent toolbar is in by reading the data from memory
                        Information:
                        Toggle between release\resume state: 0x005556FC <--- This is the address where the states are stored
                        BYTE data = *(int*)0x005556FC;
                         
                        DWORD datasize = sizeof(data);
                        Now we check the values. We know that the value will either be 0 or 1.
                        0 = release - green
                        1 = resume - flashing red
                        
                        Release mode - green
                        00 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
                        Resume mode - red flashing
                        01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
                    */
    
                    /*
                        NOTE:
                        There are 2 ways to do this. Method 1 seems to run a bit better, but I still need to find a way to optimize the code so it runs more smoothly.
                    */
    
                    /*Method 1*/
    /*
                    //We are checking to see whether or Shoretel is in Resume Mode & whether or not the Window is already minimized
                    if((int)data == 1 && minimized == true)
                    {
                        //Assuming the above statement is true, we go ahead and check to make sure if the Window is minimized
                        if((ShowWindow(hWnd, SW_MINIMIZE)))
                        {
                            //Maximize the window
                            ShowWindow(hWnd, SW_SHOWNORMAL);
                            maximized = true;
                            minimized = false;
    
                            //This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
                            if(msgShow != true)
                            {
                                MessageBoxA(NULL,"Maximized now","Mnfo",MB_OK);
                                msgShow = true;
                                msgHide = false;
                            }
                        }            
                    }
    
                    else if((int)data == 0 && maximized == true)
                    {
                        if((ShowWindow(hWnd, SW_SHOWNORMAL)))
                        {
                            //Minimize the window
                            ShowWindow(hWnd, SW_MINIMIZE);
                            minimized = true;
                            maximized = false;
    
                            //This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
                            if(msgHide != true)
                            {
                                MessageBoxA(NULL,"Minimized now","Info",MB_OK);
                                msgHide = true;
                                msgShow = false;
                            }
                        }
                    }
    */
                    /*This is just for testing purposes*/
                    //printf("Value for 0x005556FC is = %i \n", (int) data);
    
                    /*Method 2*/
    
                    //This is another way to check the state the Window is in
                    if(GetWindowPlacement( hWnd, &wd ))
                    {
                        //We store the variable inside nCurShow
                        nCurShow = wd.showCmd;
                        
                        //We check to see if Window is minimized & whether Shoretel is in resume mode
                        if((int)data == 1 /*&& minimized == true */&& nCurShow == SW_MINIMIZE)
                        {
                            //Maximize the Window and set the foreground
                            ShowWindow( hWnd, SW_SHOWNORMAL);
                            SetForegroundWindow(hWnd);
                            
                            //This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
                            if(msgShow != true)
                            {
                                MessageBoxA(NULL,"maximized now","info",MB_OK);
                                msgShow = true;
                                msgHide = false;
                            }
                        }
                        else if((int)data == 0 /*&& maximized == true */&& nCurShow == SW_SHOWNORMAL)
                        {
                            //Minimize the Window and set the foreground
                            ShowWindow( hWnd, SW_MINIMIZE );
                            SetForegroundWindow(hWnd);
                            
                            //This is optional, but the sole purpose of this is to popup a messagebox & let the user know what's going on
                            if(msgHide != true)
                            {
                                MessageBoxA(NULL,"minimized now","info",MB_OK);
                                msgHide = true;
                                msgShow = false;
                            }
                        }
                    }
    
                    else
                    {
                        //The default state it should be in is ShowNormal, which sets the application in a default state.
                        ShowWindow( hWnd, SW_SHOWNORMAL );
                        SetForegroundWindow(hWnd);
                    }
    
                    /*We want to make sure we close the Process handle so that we don't corrupt the memory access or cause any unexpected errors*/
                    CloseHandle(Process);
                }
            }
        }
    
        //On Exit, restore the window
        ShowWindow(hWnd, SW_SHOWNORMAL);
        return 0;
    }

  2. #2
    Administrator JoTo's Avatar
    Join Date
    May 2010
    Location
    www.scapp.net
    Posts
    1,953

    Default

    have not checked all the code, but it looks to me like the loop while(1) runs without having any break: loop start - processing - loop back - processing ... - , so I guess you need to insert a threading sleep at the end of the loop: loop start - processing - sleep a while - loop back - processing ... If it consumes 50% cpu I assume youre having a double core cpu system

  3. #3
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    So maybe, I should have a switch instead and use cases. That's a possibility.
    Is there a better way to run a program without having a infinite loop (while(1))?

    while(1) is just a shortcut method I've used for a while.

  4. #4
    Administrator JoTo's Avatar
    Join Date
    May 2010
    Location
    www.scapp.net
    Posts
    1,953

    Thumbs up

    having a while(1) loop is fine if thats what you want: to have a continously neverending loop in the app, I think its supposed to do check aslong the app is runnign right ? But you need to make sure that it eats not up the whole cpu, so you need to make the executing thread sleep or inserting a wait. Easiest is to insert a sleep, not entirely sure about the syntax in C:
    Code:
    while(1)
    {
    ...processing here...
    
    //now sleep a bit :)
    void Sleep (milliseconds) 
    }
    so having eg. 50 milliseconds means your app checks the other app 20 times a second, You decide if thats enough. If you don't insert a break then the app will check as often as possible = 100% cpu time.

  5. #5
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Bah thank you, that did it. Should have thought of that :P

  6. #6
    Administrator JoTo's Avatar
    Join Date
    May 2010
    Location
    www.scapp.net
    Posts
    1,953

    Default

    nice to hear. About alternatives for a while(1) loop: Another method would be to work with timers, windows offers 3 different types of times, the default one isn't very exact - same like the sleep method, but there is a system kernel timer which works very accurate something in nano second area, so if you need to work with exact timing that would be the way to choose.
    A completely different method like those polling methods would be to work event-based. For that you would catch the events, in this case from the app that needs to be controlled (windows messages, message loop), and react on them.

  7. #7
    Administrator James's Avatar
    Join Date
    May 2010
    Location
    on the intraweb
    Posts
    3,180

    Default

    Yeah makes sense. I've worked with events before, but it was in a win32 API app as opposed to a simple console app. This app is more or less something I volunteered to do for work to help them out since I have background in programming haha.

    Events would work great too yeah, but I figure, as long as it works the way it's supposed to, no need to mess with it now. I appreciate the help again Joto.

  8. #8
    Über Prodigy & Developer Razo[R]apiD's Avatar
    Join Date
    May 2010
    Location
    Poland, Lublin
    Posts
    3,257

    Default

    Yep, timers whould do the thing too.
    As I remember you set timer with simple function and you give it as a param, pointer to function which you want to execute. You also specify how often it should execute it, like every second or 2 seconds, etc.

    Here you can read about it:
    http://msdn.microsoft.com/en-us/libr...06(VS.85).aspx

Posting Permissions

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