博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dispatcher.Invoke and Dispatcher.BeginInvoke
阅读量:2437 次
发布时间:2019-05-10

本文共 2441 字,大约阅读时间需要 8 分钟。

Dispatcher.Invoke是同步执行,msdn描述:

Executes the specified delegate with the specified arguments synchronously on the thread the Dispatcher is associated with.

返回值是object, 是被调用的委托的返回值,如果该委托没有返回值,则为null。它有好几个重载方法,下面是其中之一:

public Object Invoke(	Delegate method,	paramsObject[] args)

Invoke is a synchronous operation; therefore, control will not return to the calling object until after the callback returns.

---------------------------------------------------------------------------------------------------------------------------------

Dispatcher.BeginInvoke是异步执行,msdn描述:

Executes the specified delegate asynchronously with the specified arguments on the thread that the Dispatcher was created on.

返回值是DispatcherOperation, An object, which is returned immediately after BeginInvoke is called, that can be used to interact with the

delegate as it is pending execution in the event queue. 它有好几个重载方法,下面是其中之一:

public DispatcherOperation BeginInvoke(	Delegate method,	params Object[] args)
不同点是Dispatcher.Invoke直到回调返回之后才将控制权返回给调用对象。Dispatcher.BeginInvoke是立即返回控制权给调用对象,只是把delegate加到消息循环中。
 
如下面的例子:
 
private 
void 
Window_Loaded(
object 
sender, RoutedEventArgs e)
        
{
            
Test1();
            
Test2();
            
Test3();
        
}
 
        
private 
void 
Test1()
        
{
            
// 同步的按代码顺序执行
            
Dispatcher.Invoke(
new 
Action<
string
>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.SystemIdle,
"Invoke 1"
);
            
Dispatcher.Invoke(
new 
Action<
string
>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.Send,
"Invoke 2"
);
            
// 以下3个异步,
            
Dispatcher.BeginInvoke(
new 
Action<
string
>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.Normal,
"BeginInvoke 1"
);
            
Dispatcher.BeginInvoke(
new 
Action<
string
>(Console.WriteLine), System.Windows.Threading.DispatcherPriority.Send,
"BeginInvoke 2"
);
            
// 异步默认的DispatcherPriority是Normal
            
DispatcherOperation dop = Dispatcher.BeginInvoke(
new 
Action<
string
>(Console.WriteLine),
"BeginInvoke 3"
);
            
Console.WriteLine(
"dop.Priorty: " 
+ dop.Priority.ToString());
 
            
Console.WriteLine(
"Hello normal"
);
        
}
 
        
private 
void 
Test2()
        
{
            
Dispatcher.BeginInvoke(
new 
Action(() => MessageBox.Show(
"Invoke 1: Hello world"
)));
            
MessageBox.Show(
"Invoke 2: Hello world"
);
        
}
 
        
private 
void 
Test3()
        
{
            
Dispatcher.BeginInvoke(
new 
Action(() => { MessageBox.Show(
"hello 1"
); }), System.Windows.Threading.DispatcherPriority.Background);
            
MessageBox.Show(
"hello 2"
);
        
}

输出结果:

Invoke 1

 Invoke 2
 dop.Priorty: Normal
 Hello normal
 BeginInvoke 2
 BeginInvoke 1
 BeginInvoke 3

转载地址:http://knrmb.baihongyu.com/

你可能感兴趣的文章
某山面试 3、实现如下函数:
查看>>
malloc的小知识
查看>>
UVALive 6755 - Swyper Keyboard
查看>>
uva10023 手算开方的方法
查看>>
第一个JSP程序(JSP入门)
查看>>
JSP语法简介
查看>>
JSP中EL表达式入门与简介
查看>>
Spring的几种注入方式
查看>>
Spring自动装配
查看>>
Hibernate入门与实例
查看>>
Jython入门学习
查看>>
Hiberate基础用法实例
查看>>
Maven编译时指定JDK版本
查看>>
Hibernate单向关联N-1
查看>>
Hibernate单向关联1-1
查看>>
jQuery自定义动画
查看>>
Spring-data-redis在shiro中的实例
查看>>
GUN C中__attribute__作用
查看>>
3、系统调用之SYSCALL_DEFINE分析
查看>>
linux的signal_pending及signal
查看>>