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.