简体   繁体   English

在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke

[英]Invoke or BeginInvoke cannot be called on a control until the window handle has been created

hey guys I get this exception when my application closes. 嗨,我在应用程序关闭时遇到了这个异常。 CustomerReadySub is an event that I have subscribed to. CustomerReadySub是我已订阅的事件。

The error happens on this line 该行发生错误

fTypeLabel.Invoke(new MethodInvoker(fuelTypeChosen));

public void CustomerReadySub(object sender, CustomerReadyEventArgs fuel)
    {
            // code to handle the event
            string CustReady = null;

            //checks what fuel is chosen and then activates the pump
            fuelType = fuel.SelectedFuel.ToString();

            if (!String.IsNullOrEmpty(fuelType))
            {
                fTypeLabel.Invoke(new MethodInvoker(fuelTypeChosen));

                if (fuelType == "Unleaded") //checks fuel type and displays price accordingly
                {
                    pplText.Invoke(new MethodInvoker(petrol));
                }
                else
                {
                    pplText.Invoke(new MethodInvoker(diesel));
                }

                CustReady = "READY";
                WCFPump.sendReady(CustReady);
            }

            while (WCFPump.getOK() == 0) { /*do nothing*/} //used to loop around until OK is retrieved
            if (pumpID == WCFPump.getOK())
            {
                CustGen.ActivatePump();
            }

    }

    private void fuelTypeChosen()
    {
        fTypeLabel.Text = fuelType;
    }

I am not really sure what is causing the problem. 我不太确定是什么引起了问题。

The error seems quite clear, except for the 'until' part when you are already closing down. 该错误似乎很明显,除了已经关闭的“ until”部分。 I think you can also read it as "Invoke or BeginInvoke cannot be called on a control after the window handle has been destroyed " 我认为您也可以将其读取为“在销毁窗口句柄无法在控件上调用Invoke或BeginInvoke”

So your event fires after the Window has been destroyed. 因此,在销毁Window之后,您的事件就会触发。 The actual code responsible for that is not posted but the code we do see is really happy to do busy-waiting. 负责此操作的实际代码尚未发布,但是我们看到的代码真的很乐意忙于等待。 So apparently you are disturbing the Windows event mechanism enough to cause this timing problem. 因此,显然您在干扰Windows事件机制,足以引起此计时问题。

Short fix: 简短修正:

      if (! fTypeLabel.IsHandleCreated) return;  // emergency exit
      fTypeLabel.Invoke(new MethodInvoker(fuelTypeChosen));

but you should really consider fixing the synchronous nature of your code. 但您应该真正考虑修复代码的同步特性。 Uses events and async. 使用事件和异步。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#编译错误:“在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。” - C# compile error: “Invoke or BeginInvoke cannot be called on a control until the window handle has been created.” $exception {在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。} System.InvalidOperationException - $exception {Invoke or BeginInvoke cannot be called on a control until the window handle has been created.} System.InvalidOperationException 在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - Invoke or BeginInvoke cannot be called on a control until the window handle has been created 错误:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - Error: Invoke or BeginInvoke cannot be called on a control until the window handle has been created InvokeEvent:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke - InvokeEvent: Invoke or BeginInvoke cannot be called on a control until the window handle has been created 我只能关闭一次表单,在创建窗口句柄之前,无法在控件上调用InvalidOperation Exception Invoke或BeginInvoke - I can only close a form once, InvalidOperation Exception Invoke or BeginInvoke cannot be called on a control until the window handle has been created VS 2022 C# Forms:System.InvalidOperationException:“在创建 window 句柄之前,无法对控件调用 Invoke 或 BeginInvoke。” - VS 2022 C# Forms: System.InvalidOperationException: 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.' 我只能关闭一次表单,在创建窗口句柄之前,无法在控件上调用InvalidOperation Exception Invoke或BeginInvoke - I can only close a form once, InvalidOperation Exception Invoke or BeginInvoke cannot be called on a control until the window handle has been created “在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke”仅在第二次打开表单时出现 - “Invoke or BeginInvoke cannot be called on a control until the window handle has been created” only occurs the second time form opens 在创建 window 句柄之前,无法在控件上调用 c# Invoke 或 BeginInvoke System.Threading.Thread - c# Invoke or BeginInvoke cannot be called on a control until the window handle has been created System.Threading.Thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM