hi all i have my program that creates a task that runs 10 min after the time its created and i was wondering how i could have the program monitor till when the task is run then excute the rest of the code
Printable View
hi all i have my program that creates a task that runs 10 min after the time its created and i was wondering how i could have the program monitor till when the task is run then excute the rest of the code
What type of "task" is it?
a task using the task scheduler that open up another program at the specified time
Try something like this...
VB Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Sub Command1_Click() 'start code Sleep (60000 * 10) 'in milli-seconds 'contnue code End Sub
Is it your program or not?Quote:
Originally Posted by dark_shadow
Are you Shelling that 'Task' (from your App). If so you may be able to use the WaitForInputIdle() API.
@RhinoBull no the program i'm opening is not my own
@ Bruce Fox i'm creating the task by shelling the "At" command in command prompt
now considering it was a windowed app could i just the program search for the hwnd of the window or the caption of the window?
You can certainly find window if you know its caption but if it's not consistent then you may have trouble looking for one.
ah i think i may have solved the problem i found an example on how to find part of the windows caption, there is one word that is always in the title bar of this window so i'll try it this way and see if it works if not i'll get back to you :wave: thanks for the help/suggestions so far guys
Wow, that is a risk if it is a common word and could appear in other titles :)
Exactly right Bruce: there could be many Notepad windows with similar captions "Notepad - nobody knows what..." and FindWindow will return first available. Then what...
well in the title it shows the path of where it executes for example to use command prompt as an example it shows C:/Windows/System32/cmd.exe it shows it like that as well so i just search for the last part ( in this example cmd.exe)but as you guys are more experienced than i am i'm open for suggestions on better ways :) and thanks again :wave:
I hope it works for you but it's very risky to use partial caption you may end up executing second part of your main program too early.
so what would be a better way? maybe searching for it by class ?
Or, perhaps you could create a wrapper for the other program and use that as your scheduled task instead. Then launch the other program from it using ShellAndWait or something similiar and notify your program when it is finished.
Just a thought.
I tried a few here too: http://www.vbforums.com/showthread.p...highlight=wait
the program that is ran by the task needs to stay open i want to check for when that program is open. i think i didnt explain my question well enough, so here's a basic run down of what the program should do : it creates a task using the "at" command that opens an external program in 10 min time from when it application is ran. when the task is completed and the other program is ran then my program should excute the rest of the code. ex: my program is run at 1:00 makes a task for notepad to run at 1:10pm thenit waits until notepad is run then it executes the rest of the code
You will to sit there and wait till that 3rd party app starts, and reqularily check for an instance of it, possibly using FindWindow etc.
so what would be the best method to do that ? look for the creation of the window? or is there an easier way?
You could place a Timer on the Form, and set it's interval to 1 minute. Then check for your spawned app (each minute) once identified, disable the Timer and get on with business.
ah so for as for the actual checking for the spawned app what would you suggest using ? looking for the window or looking for the application itself ? i was thinking the application itself acually
Outa curiosity, what is the name of this App?
FindWindow... or equivalent (as it's Thread specific from memory). Is there a reason you couldn't use similar?
Window Title
Module:
VB Code:
Option Explicit Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _ (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long Private Declare Function EnumWindows Lib "user32.dll" _ (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Private Target As String Private IsOpen As Boolean Private Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long Dim buf As String * 256 Dim title As String Dim length As Long length = GetWindowText(app_hWnd, buf, Len(buf)) title = Left$(buf, length) If InStr(1, title, Target, vbTextCompare) Or _ title Like Target Then IsOpen = True End If EnumCallback = 1 End Function Public Function WindowExists(app_name As String) As Boolean IsOpen = False Target = app_name EnumWindows AddressOf EnumCallback, 0 WindowExists = IsOpen End Function
Form:
VB Code:
If WindowExists("Notepad") Then Debug.Print "Notepad is open" Else Debug.Print "Notepad is not open" End If
Program EXE
VB Code:
Private Sub Command1_Click() If ExeExists("Notepad.exe") Then Debug.Print "Notepad is open" Else Debug.Print "Notepad is not open" End If End Sub Private Function ExeExists(app_exe As String) As Boolean Dim Process As Object For Each Process In GetObject("winmgmts:").ExecQuery _ ("Select Name from Win32_Process Where Name = '" & app_exe & "'") ExeExists = True Next End Function
@bruce Fox the app i'm trying to open is photoshop