本文介绍了调用需要的疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将从非UI线程调用以下方法.我是否应该检查InvokeRequired,以便在方法中调用这些项?
The following method will be invoked from a non UI thread. Should I check InvokeRequired, for calling these items in the method?
a. this._moduleStatusGrid.Invalidate()
b. this.Close()
a. this._moduleStatusGrid.Invalidate()
b. this.Close()
private void CheckIfAllModulesInitComplete()
{
this._moduleStatusGrid.Invalidate();
if (this._moduleDataList.Count(moduleData => !moduleData.IsInitOver) == 0)
{
this._footprint.DeActivate();
this.Close();
}
}
推荐答案
通过UI线程和非UI线程可以安全地调用Control.Invoke和Control.BeginInvoke,因此如果您已经知道自己在非UI上线程没有危害(IMO)跳过检查,仅调用Invoke/BeginInvoke.
Control.Invoke and Control.BeginInvoke are safe to call from the UI thread and non-UI threads, so if you already know you are on a non-UI thread there is no harm (IMO) skipping the check and just calling Invoke/BeginInvoke.
示例:
anyControl.Invoke((MethodInvoker)delegate{
// anything to run on UI thread here
});
这篇关于调用需要的疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!