百度网站推广找谁做,安徽网站建设服务平台,在哪里安装wordpress,购物网站主页模板form:https://www.jianshu.com/p/0714fc755988之前的文章说过了如何使用BackgroundWorker#xff0c;今天要说的是WPF程序员处理多线程的另外一个方式 - Dispatcher当我们打开一个WPF应用程序即开启了一个进程#xff0c;该进程中至少包含两个线程。一个线程用于处理呈现今天要说的是WPF程序员处理多线程的另外一个方式 - Dispatcher当我们打开一个WPF应用程序即开启了一个进程该进程中至少包含两个线程。一个线程用于处理呈现隐藏在后台运行一个线程用于管理用户界面接收输入、处理事件、绘制屏幕以及运行应用程序代码。即UI线程。在UI线程中有一个Dispatcher对象管理每一个需要执行的工作项。Dispatcher会根据每个工作项的优先级排队。向Dispatcher列队中添加工作项时可指定10个不同的级别。那么问题来了如果遇到耗时操作的时候该操作如果依旧发生在UI线程中Dispatcher 列队中其他的需要执行的工作项都要等待从而造成界面假死的现象。为了加快响应速度提高用户体验我们应该尽量保证Dispatcher 列队中工作项要小。所以对于耗时操作我们应该开辟一个新的子线程去处理在操作完成后通过向UI线程的Dispatcher列队注册工作项来通知UI线程更新结果。Dispatcher提供两个注册工作项的方法Invoke 和 BeginInvoke。这两个方法均调度一个委托来执行。Invoke 是同步调用也就是说直到 UI 线程实际执行完该委托它才返回。BeginInvoke是异步的将立即返回。Dispatcher实际上并不是多线程子线程不能直接修改UI线程必须通过向UI线程中的Dispatcher注册工作项来完成Dispatcher 是单例模式暴露了一个静态的CurrentDispatcher方法用于获得当前线程的Dispatcher每一个UI线程都至少有一个Dispatcher一个Dispatcher只能在一个线程中执行工作。开启新线程的方法很多比如delegate.BeginInvoke()的方式开启的新线程。Delegate.Invoke: Executes synchronously, on the same thread.Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.示例程序XAMLWindow x:ClassDispatcherExample.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleMainWindow Height160 Width300StackPanelProgressBar NameprogressBar Height20 Width250 Margin10/ProgressBarTextBox NametextBox Width50 Height20 HorizontalAlignmentCenter/TextBoxButton NamebtnProcess Width100 ClickbtnProcess_Click Margin5Start/ButtonButton NamebtnCancel Width100 ClickbtnCancel_Click Margin5Cancel/Button/StackPanel
/Window
C#namespace DispatcherExample
{/// summary/// Interaction logic for MainWindow.xaml/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();}Thread taskThread;private void btnProcess_Click(object sender, RoutedEventArgs e){taskThread new Thread(DoTask);taskThread.Start();}private void btnCancel_Click(object sender, RoutedEventArgs e){taskThread.Abort();MessageBox.Show(Background task finished normally, info);this.progressBar.Value 0;this.textBox.Text null;}private void DoTask(){Int64 InputNum (Int64)100;for (Int64 i 0; i InputNum; i){Thread.Sleep(100);this.Dispatcher.BeginInvoke((Action)delegate(){this.progressBar.Value i;this.textBox.Text i.ToString();//显示百分比值});}MessageBox.Show(Background task has been canceled, info);this.Dispatcher.BeginInvoke((Action)delegate(){this.progressBar.Value 0;this.textBox.Text null;});}}
}
演示DispatcherExample.gif作者Jason_Yuan链接https://www.jianshu.com/p/0714fc755988來源简书著作权归作者所有。商业转载请联系作者获得授权非商业转载请注明出处。---------------------------------------------------------------------------------------------------------------------------C#中的委托(delegate)用法简介委托 delegate是只有一个函数的特殊的类委托对象的引用相当函数指针
delegate 声明定义一种引用类型该类型可用于将方法用特定的签名封装。委托实例封装静态方法或实例方法。
委托大致类似于 C 中的函数指针但是委托是类型安全和可靠的。
委托使您得以将函数作为参数传递。委托的类型安全要求作为委托传递的函数拥有同委托声明相同的签名使用委托使程序员可以将方法引用封装在委托对象内。
然后可以将该委托对象传递给某个方法而不必在编译时知道将调用哪个方法。与 C 或 C 中的函数指针不同委托是面向对象、类型安全的
委托声明定义一种类型它用一组特定的参数以及返回类型封装方法。对于静态方法委托对象封装要调用的方法。对于实例方法委托对象同时封装一个实例和该实例上的一个方法。如果您有一个委托对象和一组适当的参数则可以用这些参数调用该委托。 委托的一个有趣且有用的属性是它不知道或不关心自己引用的对象的类。任何对象都可以只是方法的参数类型和返回类型必须与委托的参数类型和返回类型相匹配。这使得委托完全适合“匿名”调用//例子1
class Program
{delegate bool CompareOp(int v1, int v2);static void Main(string[] args){CompareOp less new CompareOp(Program.Less);Console.WriteLine(less(1, 2).ToString());Console.WriteLine(Test Delegate);Console.ReadLine();}static public bool Less(int a, int b){return a b;}}//例子2
delegate void MyDelegate(int i); class Program
{ public static void Main() { TakesADelegate(new MyDelegate(
DelegateFunction
)); } public static void TakesADelegate(MyDelegate SomeFunction) { SomeFunction(21); } public static void
DelegateFunction
(int i){ System.Console.WriteLine(Called by delegate with number: {0}., i); } }