通过ActiveX关闭WPF应用程序 - 未正确关闭

时间:2015-06-17 12:40:27

标签: c# wpf activex

我有一个WPF项目" ActiveX"这是为COM Visibility注册的。它有一个类" ActiveX"。我正在尝试创建另一个WPF项目的对象" MainApplication"主窗口

ActiveX类有两种方法1)初始化2)关闭

1)初始化API - 初始化并启动" MainApplication"的主窗口。 2)关闭API - 尝试关闭Initialize API创建的实例

问题是:

调用Close API时,应用程序未完全关闭。那就是Window正在关闭,但线程正在后台运行。

有人可以建议正确退出名为ActiveX的应用程序

using MainApplication;
//This interface is registered for COM
public interface IActiveX
{
    [DispId(1)]
    bool Initialize();
    [DispId(2)]
    bool Close();
}

//This class is registered for COM                   
public class ActiveX: IActiveX
{
    MainWindow mainwindow;
    Thread thread;
    [STAThread]
    public bool Initialize()
    {
        try
        {
            ThreadStart exeFunc = new ThreadStart(() =>
            {
                Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
                {
             try
            {
          mainwindow = new MainWindow();
                     mainwindow.OpenWindow();   //OpenWindow()  is the API of the MainApplication 
            }
            catch()
            {}
       }));
            });
            thread = new Thread(exeFunc);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            System.Threading.Thread.Sleep(5000);        
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }

  public bool Close()
    {
        try
        {
            //Application.Current.Shutdown();
            //Environment.Exit(0);
            success = form.CloseWindow();      //CloseWindow()  is the API of the MainApplication 
            Thread.Sleep(2000);
            //thread.Join(15000);
            //thread.Abort();
            //thread = null;
            //mainwindow = null;
            GC.Collect();
            Thread.Sleep(5000);
            return success;
        }
        catch (Exception exp)
        {
            return false;
        }
    }

MainApplication API:

    public bool OpenWindow()
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
       { 
           try
           {
               this.ShowDialog();
           }
           catch()
           {}
       }));
        return true;
    }


    public bool CloseWindow()
    {
        Dispatcher.Invoke(DispatcherPriority.Render, new Action(() =>
        {
            try
            {
                this.Close();
               Application.Current.Shutdown();
            }
            catch
            {}
        }));
        return true;
    }

0 个答案:

没有答案