Results 1 to 7 of 7

Thread: [2005] Checking if an external program is running?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    [2005] Checking if an external program is running?

    Okay, I have a piece of code that starts an external installation program:

    Process.Start(programLocation)

    The variable programLocation may be any number of locations on the hard drive or a network resource.

    The program, ideally, should do its thing, and the process should then be ended.

    At that point, I'd like to start another installation. But I should only perform one installation at a time. Is there any way I can check to see if the previous installation has ended?

    Thanks!

    Jason

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2005] Checking if an external program is running?

    Have a look at the System.Diagnostics.Process class and its 'GetProcesses', 'GetProcessesByName' methods.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    Re: [2005] Checking if an external program is running?

    Thanks for the reply.

    I looked at those two methods, and it appears that it requires a name. First, where do I find that name? And two, is it possible for there to be two processes on the same machine with the same name, possibly providing incorrect results?

    I also saw GetProcessById(), which sounds like it'd be more consistent (I guess?). But how do you know what the ID is to begin with? Can you assign IDs?

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Checking if an external program is running?

    Yes, you can have more than process with the same name running at the same time. Open up two Internet Explorers and hit Ctl-Alt-Delete, then go to the Processes tab. You'll see two name iexplore.exe. You can see your process names here.

    However, if you are starting this process yourself, then you already have your reference to it and can use that.
    Code:
        Private _proc As Process
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Start my process.
            _proc = New Process()
            _proc.StartInfo.FileName = "IExplore.exe"
            _proc.Start()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            ' Check if I started my process.
            If _proc IsNot Nothing Then
                ' Try to close my process.
                _proc.CloseMainWindow()
                ' Wait for it to exit.
                _proc.WaitForExit(5000)
                If Not _proc.HasExited Then
                    ' Just kill it if it won't close.
                    _proc.Kill()
                End If
            End If
        End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    Re: [2005] Checking if an external program is running?

    Very helpful. Thanks.

    My only problem now is that when I went through a series of programs using _proc.WaitForExit(), it didn't wait on some of the programs. :\ Specifically, it didn't wait for a setup program created in Visual Basic 2005 Express.

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] Checking if an external program is running?

    Quote Originally Posted by loosus
    Very helpful. Thanks.

    My only problem now is that when I went through a series of programs using _proc.WaitForExit(), it didn't wait on some of the programs. :\ Specifically, it didn't wait for a setup program created in Visual Basic 2005 Express.
    Hmmm. It didn't wait 5 seconds for the process to close? I don't know.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    5

    Re: [2005] Checking if an external program is running?

    It didn't.

    Does the VB Express setup program do anything weird? It seems like when I run it, it sorta goes away for a second and then comes back. Is it possible that it's somehow "killing itself" and then bringing itself back, thereby reporting to my own program that it's been killed?

Posting Permissions

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



Click Here to Expand Forum to Full Width