我在一个中使用了这个函数Windows forms
应用:
delegate void ParametrizedMethodInvoker5(int arg);
private void log_left_accs(int arg)
{
if (InvokeRequired)
{
Invoke(new ParametrizedMethodInvoker5(log_left_accs), arg);
return;
}
label2.Text = arg.ToString();
}
But in WPF
它不起作用。为什么?
在 WPF 中,Invoke
方法位于调度程序上,因此您需要调用Dispatcher.Invoke
代替Invoke
。另外,没有InvokeRequired
财产,但调度员有CheckAccess
方法(由于某种原因,它隐藏在智能感知中)。所以你的代码应该是:
delegate void ParametrizedMethodInvoker5(int arg);
void log_left_accs(int arg)
{
if (!Dispatcher.CheckAccess()) // CheckAccess returns true if you're on the dispatcher thread
{
Dispatcher.Invoke(new ParametrizedMethodInvoker5(log_left_accs), arg);
return;
}
label2.Text= arg.ToString();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)