0

Greetings,


I made a simple "restarter" app... However, I need to close multiple instances of an application. For example, if I have three windows of paint, or three windows of Word open, how do I check if it is still open before I open a new instance?

Here is my current code:

Module RestartCheck

    Sub Main()
        Dim FormName As String
        FormName = "TestMonkey"

        '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

End Module

The way I close paint is not working. It only closes ONE instance of paint, if I have multiple open. So I would like to add a check.

Should I do something like this?
If My.Application.OpenForms.Item(i).Text = FormName Then
'(keep a count here)
End If


Sorry, new to VB. This may be a silly question. Thanks for your help.

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by bbowsh54
0

Here's what I ended up doing. There is probably a much better way to do so. This seems to work fine. If you have any advice on ways to improve, please let me know. Thanks!!

Module RestartCheck

    Sub Main()
        'close paint
        'Dim proc = Process.GetProcessesByName("PaintDotNet")
        'For i As Integer = 0 To proc.Count - 1
        'proc(i).CloseMainWindow()
        'Next i
        Dim instances() As Process = Process.GetProcessesByName("PaintDotNet")
        For Each instance As System.Diagnostics.Process In instances
            instance.Kill()
        Next

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

        'open paint again; check if it is not running
        If ProcessesRunning("PaintDotNet") = 0 Then
            Dim sAppName As String, sAppPath As String
            sAppName = "PaintDotNet"
            sAppPath = "C:\Program Files\Paint.NET\PaintDotNet.exe"
            Shell(sAppPath, vbNormalFocus)
        End If

    End Sub

    Public Function ProcessesRunning(ByVal ProcessName As String) As Integer
        Try
            Return Process.GetProcessesByName(ProcessName).GetUpperBound(0) + 1
        Catch
            Return 0
        End Try
    End Function

End Module
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.