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!!!

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

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

do you get any error???

do you get any error???

Yes...

It says:
No process is associated with this object.

For the myProcess.kill

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!

So your problem is solved???

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.