Hello Process!
I just created my first Windows service. Its sole purpose in life is to look for a Firefox process once a second and, if one is found, sets its CPU affinity to CPU0.
Works like a charm. :)
In lieu of posting binaries, which is what I'd much rather be doing, here's the source code, written in C# in Visual Studio 2005 RC. The trick is adding the service installer and using installutil.exe to install the service assembly. See Adding Installers to Your Service Application on MSDN.
I love .NET. I was intimidated by all the stuff I saw in MSDN about setting affinity until I got to .NET. Mmmm. Process.ProcessorAffinity()... Auughghghghghghh...
namespace AffinityMonitor
{
public partial class AffinityMonitor : ServiceBase
{
public AffinityMonitor()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Diagnostics.EventLog.WriteEntry("AffinityMonitor", "AffinityMonitor service started");
backgroundWorker1.RunWorkerAsync();
}
protected override void OnStop()
{
System.Diagnostics.EventLog.WriteEntry("AffinityMonitor", "AffinityMonitor service stopping");
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
Console.WriteLine("Tick!");
Process[] processes = Process.GetProcessesByName("firefox");
foreach (Process p in processes)
{
p.ProcessorAffinity = new IntPtr(1);
}
System.Threading.Thread.Sleep(1000);
}
}
}
}
