views:

95

answers:

2

My program starts a process and I need to make sure it is killed before I can run the program again. To do this, I'd like to store the start time of the Process in something like a mutex that I could later retrieve and check to see if any process has a matching name and start time.

How could I do this? I don't really want to stick anything on the harddrive that will stick around after the user logs out.

For reference I'm using C# and .NET

+4  A: 

You want to store the process ID, not the process name and start time. That will make it simpler to kill the process. You can store the file in %TMP% so that it will get cleaned up when hard drive space is low.

C# code to kill the process looks like this:

    int pid = Convert.ToInt32(File.ReadAllText(pidFile));
    Process proc = Process.GetProcessById(pid);
    proc.Kill();

You can find out the %TMP% directory like this:

var tmp = Environment.GetEnvironmentVariable("TMP");

EDIT: The PID can be reused, so you will need to deal with that, too.

RossFabricant
awesome, thanks! How would I store it in %tmp% just create a filestream and write it?
Malfist
duh! I should have known, pid. *headdesk*
Malfist
Does Windows guarantee that it won't reuse process IDs? If not, you're going to need the name, too.
Jim Mischel
You probably want to use Path.GetTempPath() rather than going for the TMP environment variable. See remarks section of http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx for details.
Jim Mischel
+1  A: 

I agree with rossfabricant that the Process ID should be stored instead of a name. Process IDs are not reused until after a restart, so this should be safe.

However, I'd recommend against using the TMP environment variable for storage, and instead look at Isolated Storage. This would be more .NET oriented in terms of storage. Also, it would lower the security required to run your application.

Unfortunately, both temporary directories and isolated storage will persist after a logout, though, so you'll need logic to handle that case. (Your app can clean out the info on shutdown, however).

If you have access to the code of process you are starting, it might be better to use something like named pipes or shared memory to detect whether the application is running. This also gives you a much cleaner way to shut down the process. Killing a process should be a last resort - in general, I wouldn't build an application where the design requires killing a process if it was at all avoidable.

Reed Copsey
I do not have control over the process I'm starting. I'm starting plink (from the puTTY suite), which is why I don't want to just kill any process with the process name of plink before trying to start it. However if pid and name match, it's probably mine.
Malfist
process IDs can be re-used before a restart. It even mentions this fact in the docs for System.Diagnostics.Process - http://msdn.microsoft.com/en-us/library/system.diagnostics.process.id.aspx
Moose
Moose: Thanks for the correction :) I hadn't realized that.
Reed Copsey
I knew it only because I've been bitten hard by that one. :)
Moose