天天看点

WPF 跨线程操作辅助方法/类

WPF 跨线程操作~

public static void IfInvoke2DoSomething(System.Windows.Controls.Control targetControl, System.Action excAction) {
            if (!targetControl.Dispatcher.CheckAccess()) {
                targetControl.Dispatcher.Invoke(excAction);
            }
            else {
                excAction();
            }
        }

        public static void IfInvoke2DoSomething(System.Windows.Controls.Control targetControl, Action<object> excAction, object parame) {
            if (!targetControl.Dispatcher.CheckAccess()) {
                targetControl.Dispatcher.Invoke(excAction, new object[] { parame });
            }
            else {
                excAction(parame);
            }
}
           
eg:  IfInvoke2DoSomething( ( TextBox c = new TextBox()), (text)=> c.Text = text  , "Example");
           

继续阅读