none
avoid starting a program many times when using process start RRS feed

  • Question

  • hi

    how to avoid starting a notepad program many times when using process start in form load event
    i need code like:
    if notepad.exe is running do nothing
    else
    Process.Start ("notepad.exe")

    Thursday, December 14, 2017 6:31 PM

Answers

  • You can use Process.GetProcessByName to determine whether an app is already running:

    https://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Frank L. Smith Thursday, December 14, 2017 6:34 PM
    • Marked as answer by Majed Abbas Thursday, December 14, 2017 6:43 PM
    Thursday, December 14, 2017 6:34 PM
  •  Use the Process class to check if there is an instance of notepad opened.  If not,  then start an instance.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim p As Process = Process.GetProcessesByName("notepad").FirstOrDefault
            If p Is Nothing Then
                Process.Start("notepad")
            End If
        End Sub
     

     EDIT - Sorry Paul,  i must have taken too long to make my post.  Did not see you already recommended this.  8(


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Thursday, December 14, 2017 6:41 PM
    • Marked as answer by Majed Abbas Thursday, December 14, 2017 6:42 PM
    • Edited by IronRazerz Thursday, December 14, 2017 7:11 PM
    Thursday, December 14, 2017 6:38 PM

All replies

  • You can use Process.GetProcessByName to determine whether an app is already running:

    https://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Frank L. Smith Thursday, December 14, 2017 6:34 PM
    • Marked as answer by Majed Abbas Thursday, December 14, 2017 6:43 PM
    Thursday, December 14, 2017 6:34 PM
  •  Use the Process class to check if there is an instance of notepad opened.  If not,  then start an instance.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim p As Process = Process.GetProcessesByName("notepad").FirstOrDefault
            If p Is Nothing Then
                Process.Start("notepad")
            End If
        End Sub
     

     EDIT - Sorry Paul,  i must have taken too long to make my post.  Did not see you already recommended this.  8(


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Thursday, December 14, 2017 6:41 PM
    • Marked as answer by Majed Abbas Thursday, December 14, 2017 6:42 PM
    • Edited by IronRazerz Thursday, December 14, 2017 7:11 PM
    Thursday, December 14, 2017 6:38 PM
  •  Use the Process class to check if there is an instance of notepad opened.  If not,  then start an instance.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim p As Process = Process.GetProcessesByName("notepad").FirstOrDefault
            If p Is Nothing Then
                Process.Start("notepad")
            End If
        End Sub
     

     EDIT - Sorry Paul,  i must have taken too long to make my post.  Did not see you already recommended this.  8(


    If you say it can`t be done then i`ll try it


    Only four minutes apart. I've done that before without refreshing the page before I posted. :-0

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, December 14, 2017 8:19 PM