Jump to content

Help with a delay in the program.


vmicchia
 Share

Recommended Posts

Ok I have a program that keeps a certain window in focus(Eterm). However it does check ffor the title of the IE window in case of a certain sitation. Sometimes that site takes some time to load but in the other cases it's pretty fast. But otherwise I need the Eterm window to be in focus. So with the delay how I currently have it it will take some time to bring the eterm window back in focus. If anyone could help me I would be very appreciaive.

Here's the code:

#comments-start
This program is meant to be used with a dual monitor setup at the sales desks. It sends screen commands to the active window which should be IE. At the current time it is only set to work with IE.
#comments-end
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
;Sets the opion to not use an exact match on titles
Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase
$Form1 = GUICreate("DMFocusKeeper", 156, 253, 890, 203)
$Label1 = GUICtrlCreateLabel("Eterm Focus Keeper", 24, 8, 100, 21)
$Button1 = GUICtrlCreateButton("Start", 32, 40, 81, 25)
$Button2 = GUICtrlCreateButton("Close", 32, 80, 81, 25)
$Group1 = GUICtrlCreateGroup("Move IE Window", 16, 128, 121, 105)
$Button3 = GUICtrlCreateButton("Switch Monitor", 32, 160, 81, 25)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
If Not IsDeclared("screen") Then
Global $screen = 0
EndIf
If Not IsDeclared("start") Then
$start = 0
EndIf
$var = 0
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
  Case $msg = $gui_event_close
   Exit
  Case $msg = $Button2
   Exit
  Case $msg = $Button1
   MsgBox(0, "Program Notification", "Please do not manually open, close or move the Internet Explorer window while this program is open.")
   CheckActive()
  Case $msg = $Button3
   If $var = 0 Then
    $var = 1
    $start = 1
   EndIf
   CheckActive()
   SwitchMonitor()
  Case Else
   Sleep(10)
EndSelect
WEnd
;Our constant function to check screens
Func CheckActive()
If $var = 1 Then
  GUICtrlSetData($Button1, "Start")
  $var = 0
Else
  GUICtrlSetData($Button1, "Stop")
  $var = 1
EndIf
While $var = 1
  $msg = GUIGetMsg()
  Select
   Case $msg = $gui_event_close
    Exit
   Case $msg = $Button2
    Exit
   Case $msg = $Button1
    CheckActive()
   Case $msg = $Button3
    SwitchMonitor()
   Case Else
    ;check to see if IE is the active window
    If WinActive("[CLASS:IEFrame]") Then
     WinActivate("Eterm")
     ;pause for 5 seconds to cope for slow loading
     Sleep(5000)
     WinActivate("Eterm")
     ;get the title of the IE window
     $title = WinGetTitle("[CLASS:IEFrame]", "")
     ;This if statement checks against the title of the IE window and then takes different actions based on the given state of the program.
     If $title == "Element - Windows Internet Explorer" And $screen <> 1 Then
      ;checks to see if the IE window is active and if not it activates the window so that it gets the command.
      If Not WinActive("[CLASS:IEFrame]") Then
       WinActivate("[CLASS:IEFrame]")
      EndIf
      $screen = 1
      Send("#+{RIGHT}")
      ;MsgBox(1, "Ths is the screen variable", $screen)
     ElseIf $title <> "Element - Windows Internet Explorer" And $screen == 1 Then
      ;checks to see if the IE window is active and if not it activates the window so that it gets the command.
      If Not WinActive("[CLASS:IEFrame]") Then
       WinActivate("[CLASS:IEFrame]")
      EndIf
      $screen = 0
      Send("#+{LEFT}")
      ;MsgBox(1, "Ths is the screen variable", $screen)
      WinActivate("Eterm")
     ElseIf $title <> "Element - Windows Internet Explorer" And $screen <> 1 Then
      WinActivate("Eterm")
      $screen = 0
     ElseIf $title <> "Element - Windows Internet Explorer" And $screen == 1 Then
      $screen = 1
     EndIf
    EndIf
  EndSelect
WEnd
EndFunc   ;==>CheckActive
Func SwitchMonitor()
$stop = 1
While $stop = 1
If Not WinActive("[CLASS:IEFrame]") Then
  WinActivate("[CLASS:IEFrame]")
EndIf
Send("#+{RIGHT}")
$stop = 0
if $screen = 1 Then
  $screen = 0
Else
  $screen = 1
EndIf
WEnd
EndFunc   ;==>SwitchMonitor
Edited by vmicchia
Link to comment
Share on other sites

to be honest, i dont know what you are doing with a lot of your code, such as how you originally run a While 1 loop. in that, it calls a function, which has its own loop- While $var = 1. except, in that loop, $var is never assigned a new value, and there is no exitLoop (that i noticed, at least). what i mean to say: it is an infinite loop. so then some of your code is never executed, because it never exits that While $var = 1 loop. anyways, here is a simplified version of what i think you are trying to do here. in this, i use the AdLibRegister function, which i think would work efficiently in this program. here it is:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $ieClass = "[CLASS:IEFrame]"; you had used
Global $screen
$Form1 = GUICreate("DMFocusKeeper", 156, 253, 890, 203, BitOR($WS_POPUP, $WS_CAPTION), $WS_EX_TOPMOST)
$Label1 = GUICtrlCreateLabel("Eterm Focus Keeper", 24, 8, 100, 21)
$Button1 = GUICtrlCreateButton("Start", 32, 40, 81, 25)
$Button2 = GUICtrlCreateButton("Close", 32, 80, 81, 25)
$Group1 = GUICtrlCreateGroup("Move IE Window", 16, 128, 121, 105)
$Button3 = GUICtrlCreateButton("Switch Monitor", 32, 160, 81, 25)
Opt("GUIOnEventMode", 1)
GUICtrlSetOnEvent($Button1, "startButton")
GUICtrlSetOnEvent($Button2, "close")
GUICtrlSetOnEvent($Button3, "SwitchMonitor")
GUISetState()
While 1
Sleep(100)
WEnd
Func close()
Exit
EndFunc   ;==>close
Func startButton()
Switch GUICtrlRead($Button1)
  Case "Start"
   AdlibRegister("ieKeepActive", 250)
   GUICtrlSetData($Button1, "End")
  Case "End"
   AdlibUnRegister("ieKeepActive")
   GUICtrlSetData($Button1, "Start")
EndSwitch
EndFunc   ;==>startButton
Func ieKeepActive()
If Not WinActive($ieClass) Then WinActivate($ieClass)
Local $title = WinGetTitle($ieClass)
Select
  Case $title = "Element - Windows Internet Explorer" And $screen <> 1
   If Not WinActive("[CLASS:IEFrame]") Then WinActivate("[CLASS:IEFrame]")
   $screen = 1
   Send("#+{RIGHT}")
  Case $title <> "Element - Windows Internet Explorer" And $screen = 1
   If Not WinActive("[CLASS:IEFrame]") Then WinActivate("[CLASS:IEFrame]")
   $screen = 0
   Send("#+{LEFT}")
   WinActivate("Eterm"); in your code, you had it activate ie, then activate Eterm right away,??
  Case $title <> "Element - Windows Internet Explorer" And $screen <> 1
   WinActivate("Eterm")
   $screen = 0
  Case $title <> "Element - Windows Internet Explorer" And $screen == 1
   $screen = 1
EndSelect
EndFunc   ;==>ieKeepActive
Func SwitchMonitor()
AdlibUnRegister("ieKeepActive")
GUICtrlSetData($Button1, "Start")
If Not WinActive($ieClass) Then WinActivate($ieClass)
Send("#+{RIGHT}")
If $screen = 1 Then
  $screen = 0
Else
  $screen = 1
EndIf
EndFunc   ;==>SwitchMonitor

i havent really tested it, because i dont have a window titled eterm and dont plan on having an ie browser with a title as "Element - Windows Internet Explorer" but it should work.

i hope it helps

Edited by mischieftoo
Link to comment
Share on other sites

The loop is supposed to stay active until the stop button is pressed. Anyway I tried your code and it just continually switched between the eterm and IE windows. The loop is supposed to be infinite until the stop button is pressed. The Program works as expected the only issue is that there is that 5 second delay which accounts for the load times of that particular window. My issue I guess is there a way to insulate that delay so that the rest of the program keeps running as expected? Edited by vmicchia
Link to comment
Share on other sites

you mean where you have

WinActivate("Eterm")

;pause for 5 seconds to cope for slow loading

Sleep(5000)

WinActivate("Eterm")

you could try something like:

Do
   WinActivate("Eterm")
Until WinActive("Eterm") <> 0

but i dont see how it will help to use winActivate twice, unless you know that ie will switch back to itself immediately. then you could do

WinActivate("Eterm")

WinWaitActive("[CLASS:IEFrame]")

WinActivate("Eterm")

maybe that is what you are looking for?

Edited by mischieftoo
Link to comment
Share on other sites

Well I forgot to take that second one out. It was just a test. I just want to somehow have the program continie checking what it is and not see the effects of the delay. If the IE windows is changing quickly and is not the Element window it gets caught in the delay and does not switch to the eterm window till that delay is done.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...