4

updated

I have a problem related to Process.Start(); My program launches files as processes, like so:

Process processMonitor = new Process();    
processMonitor.StartInfo.FileName = filePath; // Example: @"C:\test.txt"
processMonitor.StartInfo.CreateNoWindow = true;
processMonitor.Exited += new EventHandler(Process_Exited);
processMonitor.EnableRaisingEvents = true;
processMonitor.Start();

// Handle Exited event and display process information. 
private void Process_Exited(object sender, EventArgs e)
{
    // This code is called on every exit, except images: (Windows Photo Viewer, *jpg, *png, *bmp etc.)
}

This successfully launches a process, notepad.exe with the correct file. Catching the Exited event also works so basically i have everything in place to monitor the close event for the process.

Now for the problem...

When doing exactly the same, but now for an image:

processMonitor.StartInfo.FileName = filePath; // Example: @"C:\test.jpg"

This is not successfull.. The process launches perfectly, But i can not detect if the process is ever closed. A little research shows me that a process called:

DLLHOST.EXE (COM Surrogate)

Is launched and i cannot detect the Exited event for this process.

Can anybody help me, or at least point me in the right direction?

Community
  • 1
  • 1
stackr
  • 2,742
  • 27
  • 44
  • You will need to post *accurate* code. You are not subscribing the Exited event, setting the CreateNoWindow to true makes no sense. – Hans Passant Jul 03 '13 at 15:00
  • I've encountered a much more strange problem that the `Process` returned from a call to `Start("http://www.google.com")` (open the default browser which is Google Chrome on my system) was `null`? I wanted to get its `MainWindowHandle`, ... but because it was `null` so I couldn't do anything with it (after starting). – King King Jul 03 '13 at 16:15
  • Try this :[How to detect a process start & end using c# in windows?](http://stackoverflow.com/a/8455896/47733) – lsalamon Jul 03 '13 at 17:30
  • Updated to make more clear, like @HansPassant asked – stackr Jul 04 '13 at 05:58

2 Answers2

0

If all other doesn't work, you can look into WMI: http://msdn.microsoft.com/en-us/library/aa394582(v=vs.85).aspx - this will require you to do some wrapping work (or use a wrapper, like the one here: http://www.codeproject.com/Articles/21971/WMI-Interface-for-NET)

Another option you can use as last resort and as a workaround only is polling for the process state, but this is really not recommended for most project, and it certainly doesn't sound like something you want to do in your project.

Itai Bar-Haim
  • 1,686
  • 16
  • 40
0

I think it has to do with the nature of an image. Opening a .txt file launches notepad whereas opening a .jpg opens a viewer. Any way to key into the viewer itself?

DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39