0

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.

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • So then start the process..? Then what methods/properties of `Process` can be used? (Also, might want to clarify what "first run" means.) –  Aug 29 '12 at 04:55
  • Running the BF3.exe I believe will open Origin, which then uses your default browser to open the web launcher. So starting BF3.exe won't actually launch the game, but bring up the Origin login. – 0_______0 Aug 29 '12 at 04:56

8 Answers8

1

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());
        }
V.J.
  • 918
  • 3
  • 18
  • 37
1

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

Community
  • 1
  • 1
Tonix
  • 177
  • 2
  • 9
0

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

Ashutosh
  • 25
  • 1
  • 12
0

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.

digg
  • 186
  • 3
0

Check this links below:

C# Process Killing - SO Post

Kill a process if it's running - dreamin code

Best Regards

Community
  • 1
  • 1
BizApps
  • 6,048
  • 9
  • 40
  • 62
0

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
    }
}
0_______0
  • 547
  • 3
  • 11
0

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);
}
Swanand
  • 4,027
  • 10
  • 41
  • 69
0

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");