1

Here is the following code...the Process.Exited event handler method is not being called...I also checked that by breakpoints and all of that.

Process f;

private void button3_Click_1(object sender, EventArgs e)
{
    f = new Process();
    f.StartInfo.FileName = "tutorial.mp4";
    f.EnableRaisingEvents = true;
    f.Exited += new EventHandler(f_Exited);
    f.Start();
}

private void f_Exited(object sender, System.EventArgs e)
{
    //some stuff not important
}
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Arrajj
  • 167
  • 2
  • 12
  • 1
    Is the process actually exiting? You seem to be playing an .mp4 file. Normally, Windows will start up some media player to do this, but after that it's usually up to the user to close the media player application. The process won't exit automatically just because it's finished playing the file. – Peter Duniho Feb 08 '15 at 21:40
  • wether it has exited manually or automatically shouldnt that pass it to the exit method? – Arrajj Feb 08 '15 at 21:42
  • Yes. But if it doesn't exit at all, you'll never see that event. Why would the process exit? Are you closing the media player? Also, I haven't checked but I'm not entirely certain that when you start a process in this way (i.e. use Windows Shell to start a program based on a file association) that the `Process` object is even actually tied to that process. You should double-check the `Process` object after you start it to make sure it is. Obviously you won't get the event if the `Process` object isn't attached to an actual process. – Peter Duniho Feb 08 '15 at 21:47
  • yup mate the process is exiting.. its just a 10 seconds video.. – Arrajj Feb 08 '15 at 21:51
  • didnt understand the double check part...can u please say it clearer,,,thx in advance – Arrajj Feb 08 '15 at 21:52
  • You need to add `f.StartInfo.Verb = "open";` to get the Exited event to fire. – Hans Passant Feb 08 '15 at 23:10

2 Answers2

1

I think this isn't possible, because it isn't guaranteed that a process is started at all when you open a file like this.

Suppose there isn't any standard program set for the file type ".mp4". Then Windows will ask the user to choose a program to open the file; but if the user cancels this and do not choose a program at all, then no process is started. Therfore I believe that in such a case the Exited event isn't fired at all because you can not rely on this.

All i can think of is to start the player directly with an appropriate command line argument, like so:

Process f;
private void  button3_Click_1(object sender, EventArgs e)
{
    f = new Process();
    f.StartInfo.FileName = "wmplayer.exe"; // or something other
    f.StartInfo.Arguments = @"c:\tutorial.exe"; // as for the wmplayer, you have to specify the whole path.
    f.EnableRaisingEvents = true;
    f.Exited += new EventHandler(f_Exited);
    f.Start();

}
private void f_Exited(object sender, System.EventArgs e)
{
    //some stuff not important
}
thegentlecat
  • 478
  • 3
  • 13
0

I just checked. When you use Process in this way, it does not attach the Process object to an actual process. For example, if you try to call Process.WaitForExit(), you will get an exception. So of course the Exited event can't be raised either; the Process object has no way to know what process even started, never mind when it exits.

If you want to do this, you either need to start the actual media player, providing the necessary command-line argument or DDE verb to it to play the specific file, or you will need to search the existing processes after you start the process, looking for the one that you believe to be the process that just started.

Note that only the former option is reliable. It is theoretically possible in the latter case that you might find some other instance of the media player (depending on which media player you are using and whether it can operate as multiple instances).

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136