4

So I have a WMV video file:

var fileName = @"C:\MyFolder\MyVideo.WMV"

and I am starting the video and getting my Process ID with the code:

var process = Process.Start(fileName);
if (process != null)
{
    processId = process.Id;
}

Although my video file starts, process is always null.

From Process.Start(string) MSDN I can see that:

Return Value Type:

System.Diagnostics.Process

A new Process that is associated with the process resource, or null if no process resource is started. Note that a new process that’s started alongside already running instances of the same process will be independent from the others. In addition, Start may return a non-null Process with its ProcessHasExited property already set to true. In this case, the started process may have activated an existing instance of itself and then exited.

It says that null is returned if no new process is started. But my process is started and null still returned. Why is this?

Community
  • 1
  • 1
JKennedy
  • 18,150
  • 17
  • 114
  • 198

1 Answers1

3

Because you can't "start a WMV file". In your scenario you rely on the OS file extension handler mappings to invoke a default application to handle it.

UPDATE

From the MSDN docs:

Use this overload to start a process resource by specifying its file name. The overload associates the resource with a new Process component. If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns null to the calling procedure.

Is it possible that some OS gizmo responsible for directing your media content request to a registered app for an extension was already running? I'd say likely, as logically it would be explorer.exe, which is always up.

UPDATE 2

Here is a screen shot from SysInternals after starting playback of a WMV file using Process.Start:

enter image description here

As you can see wmplayer opens under control of svchost.exe, so at the time you requested the WMV file, svchost was already up, thus Start, returns null as per design. PPT, or rather PowerPoint, will open in a separate process, not under control of svchost.

Darek
  • 4,687
  • 31
  • 47
  • 2
    This doesn't really answer the question. If I start a powerpoint file the Process returns fine and I can get the ID? – JKennedy May 20 '15 at 14:17
  • @user1 actually it does. Media player may have been opened already. Running minimized to the taskbar counts as already opened. You should check the list of running applications using Task Manager before calling `Process.Start` to see what is going on. – Panagiotis Kanavos May 20 '15 at 15:42
  • @PanagiotisKanavos No. Even if you kill media player and start it again, it returns `null` despite of new process is started – Sriram Sakthivel May 20 '15 at 15:46
  • A media file can be played in many containers, a PPT requires specific app. – Darek May 20 '15 at 15:48
  • To confirm @SriramSakthivel's comment no Windows Media Player instances are running at time of calling `Start`. Surely all files use OS file extension handlers so why would WMV be different? For example I open .txt files with SublimeText not Notepad – JKennedy May 20 '15 at 15:57
  • @SriramSakthivel yes and no. If you use process explorer you will see that `wmplayer.exe` runs as a service under an existing instance of `svchost.exe`. No process resource was created so `Process.Start` returns `null`. – Panagiotis Kanavos May 20 '15 at 15:57
  • @user1 as I just posted - no process was created. Windows started wmplayer under an existing service – Panagiotis Kanavos May 20 '15 at 15:58
  • @user1 ... This has nothing to do with the file handlers. I suspect this is done because multimedia playback has special threading requirements that go beyond realtime - it **must** execute at specific intervals or drop a packet, even if it runs in the background, as in this case. It's better to lose an audio packet or video frame than have even a slight delay. – Panagiotis Kanavos May 20 '15 at 16:13
  • @PanagiotisKanavos This seems like a logical explanation and fits nicely. Although I am struggling to find the instance of wmplayer.exe running under Service Host using Task Manager (Windows 8.1). Thanks for the information though seems to explain it. Would be good to update Darek's answer – JKennedy May 20 '15 at 16:17
  • @PanagiotisKanavos I can see new process is created and new process Id is generated for the `wmpplayer.exe`. – Sriram Sakthivel May 21 '15 at 06:49