none
How To Change a Label1.Text When a Process is Running VB.net

    Question

  • My objective is to have a Textbox act as a status for when a process such as "notepad" is running.

    My current code: (Doesn't Work)

            Dim p() As Process

            Private Sub CheckIfRunning()
                p = Process.GetProcessesByName("notepad")
                If p.Count > 0 Then
                    Label1.Text = "Status: Process Found!"
                Else
                    Label1.Text = "Status: Process Not Found!"
                End If
        
            End Sub
    Friday, March 10, 2017 11:09 PM

All replies

  • My objective is to have a Textbox act as a status for when a process such as "notepad" is running.

    Do you mean that you get the message "Status: Process Not Found!" when Notepad is open and you expected to get the message "Status: Process Found!"?  In that case, use GetProcesses() and display the list in a temporary listbox.  That will show you what you need to use to get a process by name.  You will probably need to use something like .Contains instead of "=".   If you mean that something else is happening, describe that in detail.

    Saturday, March 11, 2017 12:17 AM
  • No, When notepad is open, the label will display "Status: Process Found!" , and when its closed, it will display "Status: Process Not Found!" .

    I'm not quite sure whats your throwing at me, could you please paste it's code?

    - Any help thanks

    Saturday, March 11, 2017 12:31 AM
  • The example works for me?

    What do you mean it does not work? Error? Wrong answer?

    Saturday, March 11, 2017 12:58 AM
  • The example works for me?

    What do you mean it does not work? Error? Wrong answer?

    It's not changing my labels.
    Saturday, March 11, 2017 1:07 AM
  • No, When notepad is open, the label will display "Status: Process Found!" , and when its closed, it will display "Status: Process Not Found!" .

    Then you need to explain what you mean by "My current code: (Doesn't Work)".  What you describe is what I would expect it to do.

    Saturday, March 11, 2017 1:43 AM
  • If it's WPF it's Label.Content = "Yao Sing Yo!"
    Saturday, March 11, 2017 1:54 AM
  • No, When notepad is open, the label will display "Status: Process Found!" , and when its closed, it will display "Status: Process Not Found!" .

    Then you need to explain what you mean by "My current code: (Doesn't Work)".  What you describe is what I would expect it to do.

    The code does not do anything. It runs, but does not change labels. Personally I believe that it's not finding the process. This is because I've changed It out for Dialog boxes and still nothing happens.
    Saturday, March 11, 2017 2:05 AM
  • Hi,

    you will have to check for the process running at a small enough time interval, if you want to recognize the status change of the process.

    Use eg a timer with an interval of 1000 (= 1sec) and in its Tick handler, check for the process and updata your label.

    Regards,

      Thorsten

    • Proposed as answer by tommytwotrain Saturday, March 11, 2017 3:07 AM
    Saturday, March 11, 2017 2:14 AM
  • Hi,

    you will have to check for the process running at a small enough time interval, if you want to recognize the status change of the process.

    Use eg a timer with an interval of 1000 (= 1sec) and in its Tick handler, check for the process and updata your label.

    Regards,

      Thorsten

    Could you please explain how I could Implant that into my code Thorsten?
    Saturday, March 11, 2017 2:28 AM
  • The code does not do anything. It runs, but does not change labels. Personally I believe that it's not finding the process. This is because I've changed It out for Dialog boxes and still nothing happens.

    Insert a breakpoint at the start of the routine.  When it runs, step through a line at a time and check the values of the variables at each line.

    Create a temporary list box.  Use GetProcesses() and display the list in the listbox. Make sure that the process name you are using is the name of the process you need to find.

    Saturday, March 11, 2017 2:29 AM
  • Hi,

    you will have to check for the process running at a small enough time interval, if you want to recognize the status change of the process.

    Use eg a timer with an interval of 1000 (= 1sec) and in its Tick handler, check for the process and updata your label.

    Regards,

      Thorsten

    Could you please explain how I could Implant that into my code Thorsten?

    Oh I see. This is what Thorsten means. When you Open and Close notepad it changes the label.

    Public Class Form3
        Dim p() As Process
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = True}
    
        Private Sub CheckIfRunning()
            p = Process.GetProcessesByName("notepad")
            If p.Count > 0 Then
                Label1.Text = "Status: Process Found!"
            Else
                Label1.Text = "Status: Process Not Found!"
            End If
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            CheckIfRunning()
        End Sub
    End Class


    Saturday, March 11, 2017 3:06 AM

  • Oh I see. This is what Thorsten means. When you Open and Close notepad it changes the label.
    Public Class Form3
        Dim p() As Process
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 100, .Enabled = True}
    
        Private Sub CheckIfRunning()
            p = Process.GetProcessesByName("notepad")
            If p.Count > 0 Then
                Label1.Text = "Status: Process Found!"
            Else
                Label1.Text = "Status: Process Not Found!"
            End If
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            CheckIfRunning()
        End Sub
    End Class


    Thanks Tommy for the example, I come here just a few times a day these days, I'm involved in writing some Poisson Blending code.

    Regards,

      Thorsten

    Saturday, March 11, 2017 3:29 AM


  • Thanks Tommy for the example, I come here just a few times a day these days, I'm involved in writing some Poisson Blending code.

    Regards,

      Thorsten

    Well it was fun that you realized what Dan meant as I could not figure, and I had the code. ☺
    Saturday, March 11, 2017 3:39 AM
  • My objective is to have a Textbox act as a status for when a process such as "notepad" is running.

    My current code: (Doesn't Work)

            Dim p() As Process

            Private Sub CheckIfRunning()
                p = Process.GetProcessesByName("notepad")
                If p.Count > 0 Then
                    Label1.Text = "Status: Process Found!"
                Else
                    Label1.Text = "Status: Process Not Found!"
                End If
        
            End Sub

    I made this code and it works

    Public Class Form1
        WithEvents Ticker As New Timer With {.Interval = 1000, .Enabled = True}
        Private Sub Tick(sender As System.Object, e As System.EventArgs) Handles Ticker.Tick
            If Process.GetProcessesByName("Notepad").Count > 0 Then
                Label1.Text = "Notepad Running"
            Else
                Label1.Text = "Notepad NOT Running"
            End If
        End Sub
    End Class
    

    Saturday, March 11, 2017 4:42 AM
  •  And yet,  another example....

    Public Class Form1
        Private WithEvents Timer1 As New Timer With {.Interval = 1000, .Enabled = True}

        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Label1.Text = If(Process.GetProcessesByName("Notepad").Length > 0, "Notepad Opened", "Notepad Not Opened")
        End Sub

        Private Sub Form1_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
            Timer1.Dispose()
        End Sub
    End Class

     

     I would recommend adding the Timer to the Form in the designer instead of in the code so that it will be added to the Form`s Components and will be disposed of automatically when the form is closed.  Then the code could be as shown below.  However,  if you add the timer dynamically (in the code),  you should either add it to the forms components in the load event or dispose it when the form is closing as shown above.

    Public Class Form1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Label1.Text = If(Process.GetProcessesByName("Notepad").Length > 0, "Notepad Opened", "Notepad Not Opened")
        End Sub
    
    End Class
    


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

    • Edited by IronRazerz Saturday, March 11, 2017 5:38 AM
    Saturday, March 11, 2017 5:09 AM