Excel VSTO中的Form.Show(IWin32Window)方法在应用程序关闭时导致ThreadAbortException [英] Form.Show(IWin32Window) method in Excel VSTO causing ThreadAbortException on application close

查看:240
本文介绍了Excel VSTO中的Form.Show(IWin32Window)方法在应用程序关闭时导致ThreadAbortException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个excel加载项,并且我有一个想要成为excel窗口前面的表格.为此,我在excel功能区菜单按钮上使用NativeWindow:

I have an excel add-in and i have a form that i would like to be front of excel window. I use NativeWindow for this purpose on excel ribbon menu button like this:

public partial class MyRibbonMenu
{
    public List<Form> Forms = new List<Form>();

    private void button1_Click(object sender, RibbonControlEventArgs e)
    {
        // initialize form
        Form frm = new Form();
        frm.Text = "Test Form";
        Forms.Add(frm);

        // create the native window handle
        NativeWindow nw = new NativeWindow();
        IntPtr iptr = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
        nw.AssignHandle(iptr);

        // when close the form release the handle
        frm.FormClosed += (sender2, e2) =>
        {
            Forms.Remove(frm);
            nw.ReleaseHandle();
        };

        // show with owner
        frm.Show(nw);
    }

}

如果我在退出excel之前关闭表单,一切正常,那么效果很好.但是,如果用户要退出excel而不关闭打开的表单,则会发生ThreadAbortException异常:

If I close my form before exit from excel everything is ok, this works very well. But if user wants to exit from excel without close the opened form then ThreadAbortException exception occurs:

System.Windows.Forms.dll中出现未处理的'System.Threading.ThreadAbortException'类型异常

An unhandled exception of type 'System.Threading.ThreadAbortException' occurred in System.Windows.Forms.dll

其他信息:线程正在中止.

Additional information: Thread was being aborted.

为解决此问题,我尝试了以下代码,但没有成功:

For resolve this problem I have tried the following code but didn't work:

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
    var frms = Globals.Ribbons.MyRibbonMenu.Forms.ToArray();
    foreach (var frm in frms)
    {
        frm.Close();
    }
}

我在哪里犯错?

推荐答案

我通过使用IWin32Window接口代替NativeWindow解决了该问题:

I resolve the problem with use IWin32Window interface instead NativeWindow:

public class Win32Window : System.Windows.Forms.IWin32Window
{
    public Win32Window(int windowHandle)
    {
        _windowHandle = new IntPtr(windowHandle);
    }

    IntPtr _windowHandle;

    public IntPtr Handle
    {
        get { return _windowHandle; }
    }
}

此后,我更改下面的表单初始化代码:

After this I change form initialize code below:

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    // initialize form
    var frm = new Form();
    frm.FormBorderStyle = FormBorderStyle.FixedSingle;
    frm.MinimizeBox = false;
    frm.MaximizeBox = false;
    frm.Text = "Test Form";
    frm.StartPosition = FormStartPosition.CenterScreen;
    Forms.Add(frm);

    // create the native window handle
    var nw = new Win32Window(Globals.ThisAddIn.Application.Hwnd);

    // show with owner
    frm.Show(nw);
}

这篇关于Excel VSTO中的Form.Show(IWin32Window)方法在应用程序关闭时导致ThreadAbortException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆