Form.Show(IWin32Window) method in Excel VSTO causing ThreadAbortException on application close

核能气质少年 提交于 2019-12-06 08:15:35

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);
}
user1881587

Edit: I'm adding my answer here b\c the user answer is a work around and not an explanation of what the problem likely is: i recommend checking out the solution microsoft put out and modify it for your needs.

see my answer here: https://stackoverflow.com/a/38157476/ depending on what window you are trying to listen in on you maybe mucking up an unmanaged window listener and unfortunately NativeWindow.ReleaseHandle does not know how to restore the subclassing window chain and instead replaces the windows wndproc you altered with User32!DefWindowProc[this] which will cause an application crash or hang.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!