Label 显示Gif动画,窗口关闭偶发性抛出 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke或者 System.ComponentModel.Win32Exception (0x80004005): 创建窗口句柄时出错。

发布时间 2023-04-18 14:35:20作者: 系心一缘

2个问题如下,解决方案都一样

 

问题1

UnhandledException:System.InvalidOperationException: 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。
在 System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
在 System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args)
在 System.Windows.Forms.Label.OnFrameChanged(Object o, EventArgs e)
在 System.Drawing.ImageAnimator.ImageInfo.OnFrameChanged(EventArgs e)
在 System.Drawing.ImageAnimator.ImageInfo.set_Frame(Int32 value)
在 System.Drawing.ImageAnimator.AnimateImages50ms()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()

问题2

UnhandledException:System.ComponentModel.Win32Exception (0x80004005): 创建窗口句柄时出错。
在 System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
在 System.Windows.Forms.Control.CreateHandle()
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
在 System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args)
在 System.Windows.Forms.Label.OnFrameChanged(Object o, EventArgs e)
在 System.Drawing.ImageAnimator.ImageInfo.OnFrameChanged(EventArgs e)
在 System.Drawing.ImageAnimator.ImageInfo.set_Frame(Int32 value)
在 System.Drawing.ImageAnimator.AnimateImages50ms()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()| 在 System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
在 System.Windows.Forms.Control.CreateHandle()
在 System.Windows.Forms.Control.get_Handle()
在 System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
在 System.Windows.Forms.Control.BeginInvoke(Delegate method, Object[] args)
在 System.Windows.Forms.Label.OnFrameChanged(Object o, EventArgs e)
在 System.Drawing.ImageAnimator.ImageInfo.OnFrameChanged(EventArgs e)
在 System.Drawing.ImageAnimator.ImageInfo.set_Frame(Int32 value)
在 System.Drawing.ImageAnimator.AnimateImages50ms()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()

 

解决方案如下:

1.如果是Control页面,则 重写 Dispose,在销毁之前,先把Lable的Visible设置为false,然后把Lable image属性设置为null。

 protected override void Dispose(bool disposing)
        {
            if (LableGif != null)
            {
                LableGif.Visible = false;
                LableGif.Image = null;
            }
            if (disposing)
            {
                if (!((components == null)))
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

2.如果是Form页面。 则添加 FormClosing 事件,把Visible设置为false,图片设置为null

 private void Form01_FormClosing(object sender, FormClosingEventArgs e)
        {

            if (LableGif != null)
            {
                LableGif.Visible = false;
                LableGif.Image = null;
            }
        }