Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对控件的通用WPF多线程访问_C#_Wpf_Thread Safety - Fatal编程技术网

C# 对控件的通用WPF多线程访问

C# 对控件的通用WPF多线程访问,c#,wpf,thread-safety,C#,Wpf,Thread Safety,我在WinForms中使用了这个扩展方法,但我想知道如何用WPF实现相同的概念 public static void SafeThreadAction<T>(this T control, Action<T> call) where T : System.Windows.Forms.Control { if(control.InvokeRequired) control.Invoke(call, control); else call(co

我在WinForms中使用了这个扩展方法,但我想知道如何用WPF实现相同的概念

public static void SafeThreadAction<T>(this T control, Action<T> call)
    where T : System.Windows.Forms.Control
{
  if(control.InvokeRequired)
    control.Invoke(call, control);
  else
    call(control);
}
publicstaticvoidsafethreadaction(此T控件,操作调用)
其中T:System.Windows.Forms.Control
{
if(control.invokererequired)
控制。调用(调用,控制);
其他的
呼叫(控制);
}

我认为WPF的版本如下:

public static void SafeThreadAction<T>(this T control, Action<T> call)
    where T : System.Windows.Threading.DispatcherObject
{
  if (!control.Dispatcher.CheckAccess())
    control.Dispatcher.Invoke(call, control);
  else
    call(control);
}
publicstaticvoidsafethreadaction(此T控件,操作调用)
其中T:System.Windows.Threading.DispatcherObject
{
如果(!control.Dispatcher.CheckAccess())
control.Dispatcher.Invoke(调用,控制);
其他的
呼叫(控制);
}

Gabe,我认为调用Invoke()时不需要第二个参数,因为您已经在调用控件的调度程序。添加调用优先级参数可能也值得考虑。@Alan:第二个参数是必需的,因为
call
是一个
操作
,而不仅仅是一个
操作