AutoHotkey Community

It is currently May 26th, 2012, 10:32 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: September 4th, 2009, 7:52 pm 
Offline

Joined: August 29th, 2009, 1:05 am
Posts: 4
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]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2009, 8:02 pm 
Offline

Joined: April 4th, 2008, 8:15 pm
Posts: 538
Location: Canada
Try using a SetTimer instead of a loop


Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 4th, 2009, 10:31 pm 
Online

Joined: April 8th, 2009, 7:49 pm
Posts: 6066
Location: San Diego, California
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.
:!: 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 4th, 2009, 10:39 pm 
Offline

Joined: August 29th, 2009, 1:05 am
Posts: 4
Thank you, SetTimer worked and my code looks much cleaner too!


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], JSLover, Leef_me, Miguel, rbrtryn and 65 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group