子线程是动态任务,执行到一定程度需要在界面弹出一个弹窗,确定则进入某个界面
然而控价,弹窗,界面等,都是主线程的控制的,因为安全原因不允许跨线程。
这里就不得不使用委托
首先是跨线程的委托:
public delegate void delegate1();//定义委托 //写几个方法来操作控件: private void fun1() { if (询问弹窗) { Border_MouseLeftButtonDown(null, null);(按钮点击事件) } } 子线程中加入: Dispatcher.BeginInvoke(DispatcherPriority.Normal, new delegate1(fun1));
补充一下,wpf中,可以直接在子线程中调用控件,示例如下
this.Dispatcher.BeginInvoke((Action)delegate () { avalon_Coloredpaper.IsVisible = true; });
任务完成
参考自:
https://www.xuebuyuan.com/957469.html
然后是不同类之间委托的调用
class Program { public delegate void MyDelegate(); static void Main(string[] args) { MyDelegate myDelegate = new MyDelegate(Test.SayHello); myDelegate(); } } class Test { public static void SayHello() { Console.WriteLine("Hello Delegate!"); } }
参考自:https://blog.csdn.net/luan0125/article/details/122957212