天天看點

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");
           

繼續閱讀