[VB] Can't get window handler of process just started

Nazgulled

Centurion
Joined
Jun 1, 2004
Messages
119
I'm doing some application that when executed, starts a few others and hide their windows. For hiding the windows i'm using the ShowHindow() API but I'm having a little problem, the code is this:

Visual Basic:
Friend Const SW_HIDE As Integer = 0
Friend Const SW_RESTORE As Integer = 9

<System.Runtime.InteropServices.DllImport("user32.dll")> _
Friend Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
End Function
Dim fileProcess As Process

fileProcess = Process.Start(appPath)
fileProcess.WaitForInputIdle()

ShowWindow(CType(fileProcess.MainWindowHandle, Integer), SW_HIDE)

It works, but there's one problem... I tried this on notepad and it worked, tried on another applications and it didn't work because the MainWindowHandle was 0... if I add a System.Threading.Thread.Sleep(500) before the ShowWindow() call, it would work for most of the applications.

What can I do to make sure, every started application is hidden without having to use Sleep()? Some apps may need 0.5s others, may need a bit more or less and I would like to have something that checks the MainWindowHandle and hide it, when it's created.

Can anyone help me out?
 
Try a loop

At the very least you could use a loop:

Visual Basic:
Do While (fileProcess.MainWindowHandle = 0) 'May wish to add another exit condition (eg timeout)
    Sleep(100)
Loop

ShowWindow(CType(fileProcess.MainWindowHandle, Integer), SW_HIDE)

Good luck :cool:
 
That's what I tried before posting this but it doesn't work. For some reason, the value returned is always 0 and I don't understand why...
 
Process.WaitForInputIdle () will be better than looping as loops can eat CPU cycles and bother people running laptops on battery.

Also be aware that MainWindowHandle will always be 0 for some applications, I seem to remember reading something about this being the case if the applications main window (or first window displayed anyway) is a dialog rather than a standard window.
 
See my code on the first post, I already use Process.WaitForInputIdle() and I tried with and without Sleep() after the WaitForInputIdel() it doesn't work...

With the other programs I tested, I could get the MainWindowHandle if I wait like 500ms before trying to get it, and it worked.

But if you are saying that, what do you suggest to launch an application and get it's window handler?
 
I fixed like this:

Visual Basic:
If Not CBool(fileProcess.MainWindowHandle) Then
	Do
		System.Threading.Thread.Sleep(100)
		pWindowHandle = Process.GetProcessById(fileProcess.Id).MainWindowHandle
	Loop While pWindowHandle = 0
Else
	pWindowHandle = fileProcess.MainWindowHandle
End If

Good solution?
 
My bad, didn't read your code properly... Is this problem occurring with any application or just specific ones? Does the application(s) in question have a splash screen or similar before the main window is launched? If so that could also be causing this issue.

Have you tried using the Handle property of the object returned from Process.Start - this is the native handle for the application; you might be able to use it with the GetWindowThreadProcessId function.
 
Actually, I was only testing with notepad and some .NET 2.0 application that I did before and this application of mine was the one not working but it is now with the above code.

I even tested it with a few other applications and they all worked. I also tested with Photoshop and this one didn't work, but it's ok, who wants to hide photoshop anyway after starting it? It worked at first, I mean, the window opens and it hides but after a while it shows again... But this application is mainly for command line apps so, the above code will do I guess.

Will Process.Handle will work on ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) instead of Process.MainWindowHandle? (which is working)
 
Back
Top