大家好,
我的程序中使用到了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碰到了不可以控制的对象,因此不终止进程,直到收到特定的消息为止?怎样才能解决这个问题。望大家多多指教。