Reputation: 125
I have a form which has multiple instances of Windows Media Player COM control. After playing movies for some time it crashes. I get dialog window which tells that there is a problem with the application and it will be closed. The error report contains information about the module which caused error and it name is lavvideo.ax Probably it is some sort of codecs issue, but nevertheless I would like to catch this exception. Is it possible? The try/catch block doesn't work.
Upvotes: 1
Views: 1399
Reputation: 668
The Windows Media Player ActiveX Control is a COM component that is in-proc only if you embed in your dot net application. This COM component can pass back COM exceptions - I use it with up to 8 initantiations of the AxWindowsMediaPlayer object with no problems in my C# application. I instantiate the instances like this:
axWMPn[zi] = new AxWindowsMediaPlayer();
((ISupportInitialize)(axWMPn[zi])).BeginInit();
axWMPn[zi].Name = "Zone " + zi; // this would be the Zone name passed
axWMPn[zi].Tag = "Z" + zi; // this is a "Z" and the Zone index
axWMPn[zi].Location = loc;
axWMPn[zi].Size = sz;
Controls.Add(axWMPn[zi]);
((ISupportInitialize)(axWMPn[zi])).EndInit();
You should be able to catch most COM errors with a try catch similar to this:
private void playVideo(string file)
{
int tc6 = 0;
try
{
axWMPn[0].URL = file;
}
catch (System.Runtime.InteropServices.COMException comEx)
{
Console.WriteLine("playVideo COMException 0: " + comEx.Source + " -- " + comEx.Message);
}
I can play video and statics to these AxWMP instances simultaneously without a problem - many of the files are set to loop, some are controlled manually by various means.
Upvotes: 1
Reputation: 804
Exception that happen in native code COM or pInvoke cannot be caught from the .NET runtime environment. COM does not support propagation of exception from the callee (that could be implemented in any language) to the caller, the reason is that exception raising and catching mechanism needs involvement or support from both the callee and the caller, which becomes impossible as COM is language (C# or C++) agnostic.
Just for the sake of completion, I would like to say that an out-of-proc COM object would help save your application from crashing and become aware of the COM object crashing and probably re-instantiating them. Frequently out-of-proc COM objects instantiation fail on most COM object implementations (due to assumptions and short cuts) - Ah where is location transparency?
Upvotes: 1