zoukankan      html  css  js  c++  java
  • 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。

    public partial class UCInfo : UserControl
        {
            public UCInfo()
            {
                InitializeComponent();
               
            }
            ManualResetEvent neverSetEvt = new ManualResetEvent(false);
            delegate void InvokeDelegate();
            public void SetInfo(string info)
            {
                lblInfo.Invoke(new InvokeDelegate(() => { lblInfo.ForeColor = Color.Green; }), null);
                lblInfo.Invoke(new InvokeDelegate(() => { lblInfo.Text = info; }), null);
                ThreadPool.RegisterWaitForSingleObject(neverSetEvt, (obj, bol) =>
                    {
                        if (lblInfo.IsHandleCreated)
                        {
                            lblInfo.Invoke(new InvokeDelegate(() => { lblInfo.Text = ""; }), null);
                        }
                    }, null, 600000, true);
            }
            public void SetInfo(string info, Color color)
            {
                lblInfo.Invoke(new InvokeDelegate(() => { lblInfo.ForeColor = color; }), null);
                lblInfo.Invoke(new InvokeDelegate(() => { lblInfo.Text = info; }), null);
                ThreadPool.RegisterWaitForSingleObject(neverSetEvt, (obj, bol) =>
                {
                    if (lblInfo.IsHandleCreated)
                    {
                        lblInfo.Invoke(new InvokeDelegate(() => { lblInfo.Text = ""; }), null);
                    }
                }, null, 600000, true);
            }
        }

    当调试运行中突然关闭软件时,
    lblInfo.Invoke(labchange);语句就出先了“在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。”错误。
    解决办法是在lblInfo.Invoke(labchange);前加一个if(lblInfo.IsHandleCreated)判断就可以了

      

  • 相关阅读:
    【HDOJ】5179 beautiful number
    【HDOJ】3459 Rubik 2×2×2
    【HDOJ】3419 The Three Groups
    【HDOJ】3085 Nightmare Ⅱ
    【HDOJ】3451 Beat drop
    【HDOJ】3309 Roll The Cube
    WP手机使用记录
    WinCE系统下应用崩溃原因的分析方法
    指针减法
    WinCE Overlay
  • 原文地址:https://www.cnblogs.com/94cool/p/3904076.html
Copyright © 2011-2022 走看看