[RESOLVED] Having a problem with window state
Hello! I have a small program I wrote to open a program, close, then reopen itself when the program is closed.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Used to Open/View passwords
Process.Start("E:\Programs\KeePass\KeePass1.exe")
System.Threading.Thread.Sleep(1000)
Dim myProcesses() As Process
myProcesses = Process.GetProcessesByName("KeePass1")
If myProcesses.Count > 0 Then
Me.WindowState = FormWindowState.Minimized
Me.Visible = False
End If
Do Until Me.WindowState = FormWindowState.Normal
Dim myProcesses2() As Process
myProcesses2 = Process.GetProcessesByName("KeePass1")
If myProcesses2.Count = 0 Then
Me.WindowState = FormWindowState.Normal
Me.Visible = True
End If
Loop
End Sub
It works perfectly, but when the program reopens, it opens in this weird state where it appears in the taskbar, but will not show on the screen unless I right click on it and click maximize. The weird thing is that it works perfectly with another program. Anyone have any idea what's wrong?
Re: Having a problem with window state
That is horrible code I'm afraid. What you have there is called a "busy wait" loop and it's one of the worst possible things you can create in code. That Do loop is going to be soaking up processor cycles continually, even though the application is not doing anything useful. What you should be doing is keeping a reference to the Process object returned by Process.Start and handling its Exited event. That way your app does exactly nothing the whole time the other application is running and then receives notification only when that app exits. It's like the difference between you staying up all night watching your clock so that you know when it's time to get up and just going to sleep and letting your alarm wake you up when it's time.
Re: Having a problem with window state
For something that simple couldn't you just change the forms visible property and use .WaitForExit on the process?
Code:
Me.Visible = False
Dim myProcess As New Process
myProcess.StartInfo.FileName = "E:\Programs\KeePass\KeePass1.exe"
myProcess.Start()
myProcess.WaitForExit()
Me.Visible = True
Re: Having a problem with window state
Quote:
Originally Posted by
jmcilhinney
That is horrible code I'm afraid. What you have there is called a "busy wait" loop and it's one of the worst possible things you can create in code. That Do loop is going to be soaking up processor cycles continually, even though the application is not doing anything useful. What you should be doing is keeping a reference to the Process object returned by Process.Start and handling its Exited event. That way your app does exactly nothing the whole time the other application is running and then receives notification only when that app exits. It's like the difference between you staying up all night watching your clock so that you know when it's time to get up and just going to sleep and letting your alarm wake you up when it's time.
Yes, I had guessed it was horrible code. I'm still pretty new to vb.net. Thanks for the solution, I was not really sure if this could even be done in the way you described.
Re: Having a problem with window state
Quote:
Originally Posted by
Edgemeal
For something that simple couldn't you just change the forms visible property and use .WaitForExit on the process?
Code:
Me.Visible = False
Dim myProcess As New Process
myProcess.StartInfo.FileName = "E:\Programs\KeePass\KeePass1.exe"
myProcess.Start()
myProcess.WaitForExit()
Me.Visible = True
This is so awesome, thanks! It not only works perfectly, but also doesn't take any resources like the other person mentioned. Thanks again!
Re: Having a problem with window state
Quote:
Originally Posted by
Edgemeal
For something that simple couldn't you just change the forms visible property and use .WaitForExit on the process?
Code:
Me.Visible = False
Dim myProcess As New Process
myProcess.StartInfo.FileName = "E:\Programs\KeePass\KeePass1.exe"
myProcess.Start()
myProcess.WaitForExit()
Me.Visible = True
Quite probably.