首页 > 其他分享 >Label 显示Gif动画,窗口关闭偶发性抛出 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke或者 System.ComponentModel.Win32Except

Label 显示Gif动画,窗口关闭偶发性抛出 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke或者 System.ComponentModel.Win32Except

时间:2023-04-18 14:48:19浏览次数:44  
标签:控件 窗口 Windows 句柄 Object System Forms Threading ExecutionContext

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

 

标签:控件,窗口,Windows,句柄,Object,System,Forms,Threading,ExecutionContext
From: https://www.cnblogs.com/nmgsyps/p/17329445.html

相关文章

  • 高级控件RecycleView 设置Match_Parent 失效的解决办法
    解决方案:1.item使用RelativeLayout布局,并且布局中的view至少有一个layout_alignParentRight=true2.在adapte中的onCreateViewHolder,使用如下代码来获得View:publicViewHolderonCreateViewHolder(ViewGroupparent,intviewType){//下面这种办法会引起Match_Pa......
  • 界面控件DevExpress WPF甘特图组件,让项目管理拥有极佳性能!
    DevExpressWPF Gantt(甘特图)控件允许开发者在任何WPF桌面应用程序中快速集成项目计划和任务调度功能。在上文中(点击这里回顾>>)我们介绍了DevExpressWPF甘特图的性能、动态缩放等,本文将继续分享甘特图的其他功能,持续关注我们获取更多产品中文资讯哦~DevExpressWPF拥有120+个控......
  • 239. 滑动窗口最大值
    设计单调栈classSolution{classMyQueue{Deque<Integer>deque=newLinkedList<>();//弹出元素时,比较当前要弹出的数值是否等于队列出口的数值,如果相等则弹出publicvoidpoll(intval){if(!deque.isEmpty()&&val==deque.pee......
  • 如果是不创建新浏览器窗口,直接在已打开的浏览器,则代码需要修改为:
    如果是不创建新浏览器窗口,直接在已打开的浏览器,则代码需要修改为:caps=DesiredCapabilities.CHROMEcaps['loggingPrefs']={'performance':'ALL'}options=webdriver.ChromeOptions()options.add_experimental_option('w3c',False)options.add_experiment......
  • 通过一个具体的例子,深入了解 SAP UI5 控件数据双向绑定的工作原理和问题排查方法试读
    笔者知识星球里有朋友提问:SAPUI5中的sap.m.select控件,在当前页面做任何操作时,都可以选中key值,但触发dialog之后,可以看到select框内的key值被清空,当关闭dialog后,再去选择key值,无任何反应,请问是select控件的问题还是dialog的问题又或者是odata写法有问题,谢谢!......
  • flomo 窗口置顶 - 通用方法 autohotkey
    需求开网页的时候需要记录一些东西想一直显示操作要安装https://www.autohotkey.com/创建个.ahk文件运行下快捷键是alt+小键盘8;置顶当前窗口!Numpad8::winset,AlwaysOnTop,,AReturn使用打开flomo为当前激活,然后按快捷键即可。----------------------------------......
  • C#Winform窗体操作类&控件属性类
    WinForm磁性窗体吸附边缘1#region磁性窗体23publicclassMagneticManager4{5publicclassChildFormInfo6{7publicFormChild{get;set;}8publicMagne......
  • iframe中在父窗口打开子页面
     我们在做页面框架的时候,通常会采用一个iframe来显示子页面,但有这么种情况,就是session失效时,登录页面就会显示在iframe中,这不符合常理,一般应该显示在顶部才对。下面的js代码可以解决上述问题,只需要在登录页面添加如下代码:<scripttype="text/javascript"> /**有父窗口则在......
  • viewport 虚拟窗口
    viewport语法介绍:<metaname="viewport"content="height=[pixel_value|device-height],width=[pixel_value|device-width],initial-scale=float_value,minimum-scale=float_value,maximum-sca......
  • 香,一套逻辑轻松且智能解决PyQt中控件数值验证的问题
    在PyQt开发中,时常需要对控件的值进行校验,如需要校验QCheckBox是否被选中,QLabel是否校验值是否为空等等。在复杂的业务场景下,这类控件如果数量很多,逐个校验就显得麻烦,需要一一获得控件名称,再调用对应的方法来判断是否被选中、是否为空等。而且开发过程中如果多控件做了增减,还需要增......