In vb.net I'm trying to find a reliable way to start a new process in a hidden, minimized, normal, or maximized window (as determined ahead of time by the user).
The following code, with the value of MyWindowProcessingStyle appropriately set, works sometimes but not others:
Dim MyWindowsProcessingStyle As ProcessWindowStyle = ProcessWindowStyle.Maximized
' the above set elsewhere to values of minimized, maximized, hidden, and normal
Dim MyProcess as new Process
Dim MyProcessStartInfo As New ProcessStartInfo
With MyProcessStartInfo
.FileName = Program
.Verb = "open"
.CreateNoWindow = True
.WindowStyle = MyWindowProcessingStyle
.UseShellExecute = True
End With
myProcess.Start(MyProcessStartInfo)
For example, if I use it to start a batch file (.bat) it works reliably 100% of the time.
If however, I use it to start the window's calculator it does not really work at all. It just opens the window's calculator in the same way it was last run.
I need to stay with the other parts in the MyProcessStartInfo for reasons related to how my app works. Accordingly, this observation is no good for me.
I've tried to send keystrokes to the application, after it starts, for example (WIN)(DownArrrow) to minimize the application after the fact - and while this works in many cases it is problematic and flaky in others.
I've also tried to get the process ID or the handle of app once it has started, in the hopes that I could use various Pinvolke methods after that to get what I'm after - but I have sadly failed in that.
Any help would be very much appreciated.