954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Checking For and Closing an Application

Hello Everyone,

New to VB.NET... Going through a bunch of tutorials currently.

I need help checking if an application is open and then closing it if it is...

For an example, I'll use paint or notepad...

Does something like this work?

Dim myProcess As Process = New Process 
myProcess.StartInfo.FileName = "PaintDotNet.exe"        

myProcess.Kill()


Or should I go this route:

Private sAppName As String, sAppPath As String

sAppName = "PaintDotNet"
sAppPath = "C:\Program Files\Paint.NET\PaintDotNet.exe"

Shell(sAppPath, vbNormalFocus)


What is the best way to check if an application is open first? Struggling with that portion.

Thank you very much in advance!!!

bbowsh54
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

You can try this.

Public Function IsPRocessRunning(ByVal name As String) As Boolean
        For Each P As Process In System.Diagnostics.Process.GetProcesses
            If P.ProcessName.StartsWith(name) Then
                Return True
            End If
        Next
        Return False
    End Function
kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 

Say I have already checked, and Paint.Net is open... My goal here is to close Paint.Net, wait five seconds, and then open it again. What is wrong with this code? I'm guessing there is a different way I need to close the exe?

Sub Main()

        'close paint
        Dim myProcess As Process = New Process
        myProcess.StartInfo.FileName = "PaintDotNet"
        myProcess.Kill()

        'wait for five seconds
        Threading.Thread.Sleep(5000)

        'open paint again
        Dim sAppName As String, sAppPath As String
        sAppName = "PaintDotNet"
        sAppPath = "C:\Program Files\Paint.NET\PaintDotNet.exe"
        Shell(sAppPath, vbNormalFocus)

  End Sub
bbowsh54
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

do you get any error???

kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 
do you get any error???

Yes...

It says:
No process is associated with this object.

For the myProcess.kill

bbowsh54
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 
do you get any error???


Cool!

Tried this and it seems to work!

Sub Main()

        'close paint
        Dim proc = Process.GetProcessesByName("PaintDotNet")
        For i As Integer = 0 To proc.Count - 1
            proc(i).CloseMainWindow()
        Next i


        'wait for five seconds
        Threading.Thread.Sleep(5000)

        'open paint again
        Dim sAppName As String, sAppPath As String
        sAppName = "PaintDotNet"
        sAppPath = "C:\Program Files\Paint.NET\PaintDotNet.exe"
        Shell(sAppPath, vbNormalFocus)

    End Sub


Thanks for responses, guys!

bbowsh54
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

So your problem is solved???

kingsonprisonic
Posting Whiz in Training
265 posts since Nov 2009
Reputation Points: 47
Solved Threads: 53
 
So your problem is solved???

Yes, that issue was solved.


Now I have a new one... Expanding on that "restarter" program. Now I want to check if the program is restarted more than three times within a minute. (or any arbitrary time constraints) ... If more than three times, the restarter is ignored and the program continues running. Plus I added a feature to send a text message to myself if the restarter is used.

So now, I'm looking at some tutorials on how timing will work. Advice on best way to set a timer or clock to the program? I guess I only need to count to a minute (or a set amount of time) each time my restarter is triggered. If a count is 2 or less, and you reach 60 seconds, timer shuts off an back to the beginning of the program.

bbowsh54
Newbie Poster
8 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: