首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 使用到了COM interop后程序不能正常退出的有关问题

2012-01-22 
C# 使用到了COM interop后程序不能正常退出的问题大家好,我的程序中使用到了windowsmediaplayerActiveX组

C# 使用到了COM interop后程序不能正常退出的问题
大家好,
我的程序中使用到了windows   media   player   ActiveX组件,定义如下:
private   AxWMPLib.AxWindowsMediaPlayer   Player;
它在InitializeComponent中被初始化,并且被加入form的control集中
this.Controls.Add(this.Player);

form上有另外一个system.windows.forms.timer组件,它每隔一段时间就会调用t_Tick方法
void   t_Tick(object   sender,   EventArgs   e)

在该方法中,我判断一组条件,然后决定是否退出程序,这组条件对该问题没有影响,所以我就不说了。如果条件为真,那么就会执行如下代码
this.Dispose(true);
GC.SuppressFinalization();

我的Dispose是这样的
protected   override   void   Dispose(   bool   disposing   )
{
                        if   (disposing)
                        {
                                if   (components   !=   null)
                                {
                                        components.Dispose();
                                }
                        }
                        try
                        {
                                //   remove   the   player   from   form   control   list,   so   the   form   could   be   disposed
                                this.Controls.Remove(Player);
                                //   release   the   player   com   object
                                Marshal.ReleaseComObject(Player.GetOcx());
                                base.Dispose(disposing);
                        }
                        catch
                        {
                        }
}

我在其中用ReleaseComObject方法释放掉COM组件。

Dispose被执行完之后,就会执行到Main函数
static   void   Main(string[]   args)  
{
                        //   Adds   event   handler   to   catch   any   exceptions   that   happen
                        AppDomain.CurrentDomain.UnhandledException   +=   new   UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                        try
                        {


                                Application.Run(new   Form1(args));
                        }
                        catch   (Exception   e)
                        {
                                writeToLog( "Exception   caught   in   Main: "+e.ToString());
                        }
                       
}

但是我发现,Main执行完成后进程并没有退出,在Visual   Studio里面并没有退出Debug状态,这种情况大约持续2分钟左右,然后进程就退出了,Visual   Studio复原。

我尝试在Main中try   catch后加finally,并且执行Application.Exit();无效。
我也尝试在finally中执行Enviroment.Exit(0);这句语句执行需要2分钟左右。
因此我估计问题是处在了Environment.Exit上。

为什么明明Main已经执行完成了,进程却没有中止?是不是由于我用到了COM组件,CLR碰到了不可以控制的对象,因此不终止进程,直到收到特定的消息为止?怎样才能解决这个问题。

望大家多多指教。

[解决办法]
个人觉得:

这个ActiveX播放器本身占用大量Menory,再加上你是在调试状态下,系统资源更被大量占用!找一台2G内存的机器测试一下!


[解决办法]
up

热点排行