Daniel Lip
Daniel Lip

Reputation: 11321

How can i check/detect when i run a new exe process?

For example the game battlefield 3. The file name is bf3.exe I want that my application will detect when i first run the game and then when i exited the game.

I did in my Form1:

private ProcessStartInfo bf3;

In the constructor:

 bf3 = new ProcessStartInfo("bf3.exe");

Im not sure if using ProcessStartInfo is good idea and what to do next.

Edited:

This is my code now:

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


namespace CpuUsage
{
    public partial class Form1 : Form
    {

        private DateTime dt;
        private DateTime dt1;
        private PerformanceCounter theCPUCounter;
        private PerformanceCounter theMemCounter;
        private PerformanceCounter specProcessCPUCounter;
        private float cpuUsage;
        private float memUsage;
        private List<float> Values;

        public Form1()
        {
            InitializeComponent();

            isProcessRunning();

            dt = DateTime.Now;


            Values = new List<float>();
                theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                theMemCounter = new PerformanceCounter("Memory", "Available MBytes");
                specProcessCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);



        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            memUsage = theMemCounter.NextValue();
            label1.Text = memUsage.ToString();
            Logger.Write("Memory Usage   " + memUsage.ToString());
            cpuUsage = this.theCPUCounter.NextValue();
            label2.Text = cpuUsage.ToString();
            Logger.Write("Cpu Usage   " + this.cpuUsage.ToString());
            Values.Add(cpuUsage);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            dt1 = DateTime.Now;
            float Maximum = Values.Max();
            float Minimum = Values.Min();
            float Average = Values.Average();
            string t = string.Format("Maximum --- {0} , Minimum --- {1} , Average --- {2}", Maximum, Minimum, Average);
            Logger.Write(t);

            TimeSpan ts = (dt1 - dt);
            string time = ts.ToString(@"hh\:mm\:ss");
            Logger.Write("Time The Program Was Running ---   " + time);


        }

        private void isProcessRunning()
        {
            while (true) 
            {
                Process[] proclist = Process.GetProcessesByName("bf3.exe");
                if (proclist.Length > 0)
                {
                    Logger.Write("Battlefield 3 Started");
                }
                else
                {
                    Logger.Write("Battlefield 3 Exited");
                }
            } 

        }

    }
}

The problem now is that its entering thw while loop and never do the timer it stuck in the loop. I need to use the timer code too and the same time to check/wait to see if bf3.exe is started or ended.

Upvotes: 0

Views: 4093

Answers (8)

user4604440
user4604440

Reputation:

You need to get all process by the name and then go for a check of length.

Process[] proc = Process.GetProcessesByName("process_name");
        if (proc.Length > 0) Console.Write("Process is Running");
        else Console.Write("Not running");

Upvotes: 0

Swanand
Swanand

Reputation: 4115

If you want to measure time of that EXE run time, You can use a Stopwatch and Start it before running exe and Stop it when EXE stops.

Rough code would be:

Stopwatch run_time = new Stopwatch();
run_time.Start();

Process.Start("bf3.exe");  //You will need to enter full path here


while(true)
{
            Process[] pListofProcess = Process.GetProcesses();


            bool bRunning = false;

            foreach (Process p in pListofProcess)
            {
                string ProcessName = p.ProcessName;

                ProcessName = ProcessName.ToLower();

                if (ProcessName.CompareTo("bf3.exe") == 0)
                {
                    bRunning = true;
                }

            }
            if (bRunning == false)
            { 
                run_time.Stop();
                //Show elapsed time here.
                break;

            }
          Thread.Sleep(1000);
}

Upvotes: 0

Tonix
Tonix

Reputation: 177

In any case you want to detect a running .exe file right? In c#, maybe this links can also help you.

Detecting a Process is already running in windows using C# .net

Checking if Windows Application is running

Upvotes: 1

0_______0
0_______0

Reputation: 557

You can start it with:

System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Origin Games\Battlefield 3\bf3.exe"); //modify the start path for wherever your copy is located at

Although I'm pretty certain that running that exe only starts Origin, which then launches the web client/server selector, which starts the game.

In the method that starts it, immediately afterwards you can run an infinite loop that checks whether the "bf3.exe" process is running with something like:

while (true) // Run in a separate thread to prevent blocking if that will be a problem
{
Process[] proclist = Process.GetProcessByName("bf3.exe");
if (proclist.Length > 0)
    {
        //BF3 is running, do something here
    }
else
    {
        //BF3 isn't running any more, do something here
    }
}

Upvotes: 0

V.J.
V.J.

Reputation: 928

You want to check weather bf3.exe is running or exited or what? Then you should try following piece of code.

        Process[] _process = null;
        _process = Process.GetProcessesByName("bf3");
        foreach (Process proc in _process)
        {
            proc.Kill();
            MessageBox.Show(proc.ToString());
        }

Upvotes: 1

BizApps
BizApps

Reputation: 6130

Check this links below:

C# Process Killing - SO Post

Kill a process if it's running - dreamin code

Best Regards

Upvotes: 0

digg
digg

Reputation: 196

You can get the running processes by name with the following command:

Process[] processes = Process.GetProcessesByName("process name");

You can either periodically check, or use the hooks as mentioned in the previous answer. If you start your application after BF3, you have to get the processes once during startup.

Upvotes: 0

Ashutosh
Ashutosh

Reputation: 25

You can detect application exit as mentioned in this msdn article:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/707e9ae1-a53f-4918-8ac4-62a1eddb3c4a/

To check for start of application, u need to use hooks.

Ref: http://www.codeplex.com/easyhook

Hope this helps

Upvotes: 0

Related Questions