Search code examples
c#visual-studiowinformsbackgroundworker

C# Winforms change label based on running process


I have a dotnet 2.0 app in C# that I have developed in Visual Studio 2008, it's pretty simple:

enter image description here

I would like to change the label name currently called "label1" to "Running" or "Stopped" when my process loop.exe is running or stopped.

When I press start, it will run loop.exe and the stop button will obviously stop it.

I've read a lot of topic on C# Winforms but I cannot get this working and I have no idea what do to now. I think that I need to add a backgroundworker but I don't know how to check the process and update the label programmatically.

Here's my clean/current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace APP_NAME
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, EventArgs e)
        {
            Process.Start("loop.exe");

        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process.Start("taskkill", "/F /IM loop.exe");
            Process.Start("taskkill", "/F /IM azcopy.exe");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Process.Start("notepad.exe", @"C:\ProgramData\APP_NAME\settings\settings.xml");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", @"C:\ProgramData\APP_NAME\logs");
        }

    }
}

I hope I made myself clear, thank you.


Solution

  • You can update the text shown by a label by accessing it's Text property. So, change your method like:

    private void Start_Click(object sender, EventArgs e)
    {
        Process.Start("loop.exe");
        label1.Text = "Running";
    }