tags:

views:

324

answers:

3

If I run the following code :

Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new System.EventHandler(Process_OnExit);
myProcess.Start();

public static void Process_OnExit(object sender, EventArgs e)
{
    // Delete the file on exit
}

The event is raised when I exit notepad. If I try the same code, but I start an image instead :

Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg";
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new System.EventHandler(Process_OnExit);
myProcess.Start();

public static void Process_OnExit(object sender, EventArgs e)
{
    // Delete the file on exit
}

The event is never fired. Is it because the process that loads the image is never closed ?

UPDATE : The process to start is not always an Image. It can be anything (pdf, word document, etc). Maybe my approach isn't right. Is there any other way to delete the file after the user exited the process ?

Thank you

A: 

The event fires for me with the Microsoft Photo Viewer as the viewer. Are you possibly using a viewer that isn't actually closing?

Jonas Elfström
That's weird. I've tested the code on Windows 7 and Vista and the event is never fired. The process starts the default Windows Photo Viewer
Alexandre Pepin
Is your program exiting before the event is fired.
rerun
No. If a change my code for a filepath to a word document, the event is fired. It is really a problem with the Windows Photo Viewer.On what operating system have you tried?
Alexandre Pepin
I tested on Windows XP.
Jonas Elfström
Now I tested on Windows 7 with Picasa as viewer and it works just fine. Try to place a Console.Writeln("test") in the Process_OnExit and put a break point there but not anywhere else.
Jonas Elfström
I also tested it on Windows Xp and it is working. It is the Windows Photo Viewer of Vista and 7 that isn't firing the exited event. Really strange...
Alexandre Pepin
+1  A: 

You are using the default image viewer in windows since an image file is not executable. I changed the code to use the XP default and it worked fine.

class Program
{
    static void Main(string[] args)
    {
        Process myProcess = new System.Diagnostics.Process(); 
        myProcess.StartInfo.FileName = @"rundll32.exe"; 
        myProcess.EnableRaisingEvents = true;
        myProcess.StartInfo.Arguments = @"C:\winnt\System32\shimgvw.dll,ImageView_Fullscreen c:\leaf.jpg";
        myProcess.Exited += new System.EventHandler(Process_OnExit); 
        myProcess.Start();
        Console.Read();



    }
    public static void Process_OnExit(object sender, EventArgs e)
    {
        Console.WriteLine("called");
        Console.Read();
    } 


}
rerun
Unfortunately, I need a generic method. It may not be always an image. It can be a PDF, a word document, etc. I've updated my question.
Alexandre Pepin
+1  A: 

I would use a temp file. There are functions to create a temp file...

Your event is not firing due to the lack of the process itself, I guess. You can try to use the shell to "start" the document in question but nothing guarantees that there will be a handler for all types of files.

Gyuri
Yea I think it is a problem with the process. Thx for the help
Alexandre Pepin