Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / November 2007

Tip: Looking for answers? Try searching our database.

WairForInputIdle

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave D-C - 12 Nov 2007 22:01 GMT
Hello,
' I'm 'Shell'ing Photoshop Elements, which
' takes awhile to get settled.  Using
' Sendkeys "{END}", True and then
' WaitForInputIdle(..) does the job,
' but the Wait, although correctly waiting,
' always returns -1&=WAIT_FAILED.
' It should be 0& (or &H102&=WAIT_TIMEOUT).

' Q - why WAIT_FAILED?

' Run this routine, then open any another application.
' It prints the hWnd, Class, Title, and the
' time it takes for WaitForInputIdle.
' That can be over 1 second for Photoshop.
' This is all XL97/WIN98.  D-C Dave

' API Declarations
Declare Function GetClassName& _
 Lib "user32" _
 Alias "GetClassNameA" ( _
 ByVal hwnd As Long, _
 ByVal lpClassName As String, _
 ByVal lLen As Long)
Declare Function GetForegroundWindow& _
 Lib "user32" ()
Declare Function GetWindowText& _
 Lib "user32" _
 Alias "GetWindowTextA" ( _
 ByVal hwnd As Long, _
 ByVal lpString As String, _
 ByVal lLen As Long)
Declare Function GetWindowThreadProcessId& _
 Lib "user32" ( _
 ByVal hwnd As Long, _
 lpdwProcessId As Long)
Declare Function WaitForInputIdle& _
 Lib "user32" ( _
 ByVal hProcess As Long, _
 ByVal dwMilliseconds As Long)

Public Const WAIT_FAILED = -1&
Public Const WAIT_TIMEOUT = &H102&
Public Const INFINITE = -1&

Sub TraceForegroundWindows()
 Dim hWndNew&, hWndOld&, Pid&, Tid&, s1$, l1&, zTime1!
 Do ' forever until you stop it
   hWndNew = GetForegroundWindow()
   If hWndNew <> 0 And hWndNew <> hWndOld Then ' if new FG window,
     hWndOld = hWndNew
     
     ' print window stuff
     zTime1 = Timer ' beginning time (secs)
     Debug.Print "hWnd:  " & hWndNew & ", " & FormatSecs(zTime1)
     Debug.Print "Class: " & zGetWindowClass(hWndNew)
     Debug.Print "Title: " & zGetWindowTitle(hWndNew)
     
     ' wait for it to settle and print delay
     Pid = GetWindowThreadProcessId(hWndNew, Tid)
     SendKeys "{END}", True ' some input
     l1 = WaitForInputIdle(Pid, (INFINITE))
     ' I never get anything but WAIT_FAILED,
     ' but yet it does wait.
     If l1 <> WAIT_FAILED Then Stop
     Debug.Print "Delay: " & FormatSecs(Timer - zTime1) & vbCrLf
   End If
   DoEvents
 Loop
End Sub

Function zGetWindowClass$(phWnd&)
 Dim s1$, l1&
 s1 = Space$(260)
 l1 = GetClassName(phWnd, s1, 260)
 zGetWindowClass = Left$(s1, l1)
End Function

Function zGetWindowTitle$(phWnd&)
 Dim s1$, l1$
 s1 = Space$(260)
 l1 = GetWindowText(phWnd, s1, 260)
 zGetWindowTitle = Left$(s1, l1)
End Function

Function FormatSecs$(pT!)
 Dim HH%, MM%, SS!, sHH$, sMM$, sSS$
 HH = Int(pT) \ 3600&
 MM = Int(pT - HH * 3600&) \ 60&
 SS = pT - HH * 3600& - MM * 60&
 sHH = Format(HH, "00")
 sMM = Format(MM, "00")
 sSS = Format(SS, "00.000")
 FormatSecs = sHH & ":" & sMM & ":" & sSS
End Function
Dave D-C - 13 Nov 2007 04:02 GMT
I wrote
>      Pid = GetWindowThreadProcessId(hWndNew, Tid)
should be
      Tid = GetWindowThreadProcessId(hWndNew, Pid)
but it doesn't make any difference.  It waits on the Tid
as well as the Pid and works well but returns
WAIT_FAILED with either one.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.