dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] Having a problem with window state

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    5

    Resolved [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?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,745

    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.

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    5

    Re: Having a problem with window state

    Quote Originally Posted by jmcilhinney View Post
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    5

    Re: Having a problem with window state

    Quote Originally Posted by Edgemeal View Post
    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!

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,745

    Re: Having a problem with window state

    Quote Originally Posted by Edgemeal View Post
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width