AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fairly simple script using up 20-25% of my CPU cyles

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
jakemoffatt



Joined: 29 Aug 2009
Posts: 4

PostPosted: Fri Sep 04, 2009 6:52 pm    Post subject: Fairly simple script using up 20-25% of my CPU cyles Reply with quote

The following script takes an average of 20-25% of my CPU while it is running. Normally not even noticeable for me as I'm using a fairly quick computer. I did notice it today and think that it is excessive.

What can I do to clean it up?

I believe the CPU intensive part is probably the infinite LOOP I have.
Any better way?

What the script does is constantly looks for programs that begin with "Visual DataFlex Studio" and when it finds them, it trims off that text, so that just the "workspace" and "filename" that are loaded into the program appear on the taskbar.

In other words, I just want to remove a few characters from the front of a certain application's window title. It has to constantly loop (I believe) because each time I open a new file the application updates its window title and it needs to be renamed again.

Any tips?

Code:
#IfWinActive, Visual DataFlex 12.1 Studio

goto, run

Escape::WinClose, Visual DataFlex 12.1 Studio

RenameVDF()
{
   If WinExist("Visual DataFlex Studio")
   {
      SetTitleMatchMode, 1
      WinGetTitle, Title
      StringReplace, Title, Title, Visual Dataflex Studio 12.1 -
      WinSetTitle, %Title%     
   }
}

CloseImageMan()
{
/*
  SetTitleMatchMode, Slow
  If WinActive("ahk_class #32770", "ImageMan Developers Toolkit")
  {
    WinClose
  }
  SetTitleMatchMode, Fast
*/
}

run:

Loop
{
   RenameVDF()
   CloseImageMan()
}
[/code]
Back to top
View user's profile Send private message
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Fri Sep 04, 2009 7:02 pm    Post subject: Reply with quote

Try using a SetTimer instead of a loop
Back to top
View user's profile Send private message
Leef_me



Joined: 08 Apr 2009
Posts: 5336
Location: San Diego, California

PostPosted: Fri Sep 04, 2009 9:31 pm    Post subject: Re: Fairly simple script using up 20-25% of my CPU cyles Reply with quote

jakemoffatt wrote:
The following script takes an average of 20-25% of my CPU while it is running.
What the script does is constantly looks for programs that begin with "Visual DataFlex Studio" and when it finds them, it trims off that text, so that just the "workspace" and "filename" that are loaded into the program appear on the taskbar.

Any tips?
Arrow
You are inconsistant with your program title, note that 12.1 is shown both before and after Studio; suggest using a program title variable
(not in your case, but a simple variation like this could cause the 'test and rename' function to never rename anything.)
Code:
#   IfWinActive, Visual DataFlex 12.1 Studio
Escape::WinClose, Visual DataFlex 12.1 Studio
     If WinExist("Visual DataFlex Studio")
      StringReplace, Title, Title, Visual Dataflex Studio 12.1 -

The goto statement should be before the hotkey definitions (which includes #IfWinActive)
helpfile wrote:
The Top of the Script (the Auto-execute Section)
After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.

Code:
#IfWinActive, Visual DataFlex 12.1 Studio

goto, run

Escape::WinClose, Visual DataFlex 12.1 Studio

As written, this is like a drum-roll with no ending. I don't know the details of the underlying code, but there are a lot of instructions executed needlessly, not even a chance to 'take a breath' so to speak.
Code:
Loop
{
   RenameVDF()
   CloseImageMan()
}


See the listing below to see the tips implemented. I left out the hotkey and the coded that you had commented out.
Exclamation Please review the whole listing, there are some variations required for DFS that are not the same as for Notepad

Code:


; tested with Notepad, STMM = 2: anywhere in title
SetTitleMatchMode, 2
program = Notepad

; Untested with Visual Dataflex, STMM = 1: must start with the specified

;program = %Visual Dataflex Studio 12.1%
;SetTitleMatchMode, 1


;msgbox '- %program%'

;exitapp

loop
{
;  WinWaitActive, %program%,, 2 ; for testing purposes

;  WinWait, %program%,, 20   ; as an alternative you could trigger on existance,
            ; instead of 'active window'
  WinWaitActive, %program%,, 20
  if (ErrorLevel=1)
  {
   msgbox,,, not found, .5 ; for testing purposes
   continue
  }

  WinGetTitle, Title
  StringReplace, newTitle, Title, - %program% ; for notepad
;  StringReplace, newTitle, Title, %program% - ; for Visual DataFlex
  WinSetTitle, %Title%, , %newTitle%

}


Improvements on the above script including commenting out the 'not found' msgbox
and changing the timeout parameter of the preferred winwait??? variant.
Back to top
View user's profile Send private message
jakemoffatt



Joined: 29 Aug 2009
Posts: 4

PostPosted: Fri Sep 04, 2009 9:39 pm    Post subject: Reply with quote

Thank you, SetTimer worked and my code looks much cleaner too!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group