当我的应用程序关闭时,我得到这个异常。CustomerReadySub是我订阅的一个事件。
错误发生在这行
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;
}
我不太确定是什么引起了这个问题
错误似乎很清楚,除了'until'部分,当你已经关闭。我认为你也可以把它理解为"在窗口句柄被销毁之后,不能在控件上调用Invoke或BeginInvoke"
所以你的事件在Window被销毁后触发。负责这个的实际代码没有发布,但我们看到的代码确实很乐意做忙碌等待。很明显,你干扰了Windows的事件机制,导致了这个时间问题。
短期解决办法:
if (! fTypeLabel.IsHandleCreated) return; // emergency exit
fTypeLabel.Invoke(new MethodInvoker(fuelTypeChosen));
,但是你应该考虑修复代码的同步特性。