Dispatcher Queue
WPF消息处理队列,会根据优先级进行一一处理。
获取Dispatcher Queue的长度
var queueLength = 0;
Dispatcher.Hooks.OperationPosted += (o, e) => Interlocked.Increment(ref queueLength);
Dispatcher.Hooks.OperationStarted += (o, e) => Interlocked.Decrement(ref queueLength);
Dispatcher.Hooks.OperationAborted += (o, e) => Interlocked.Decrement(ref queueLength);
MSDN中关于DispatcherHook的定义
对于Dispatcher队列的进行的任务可进地监控上面这些任务的方式进行查看,添加到Dispatcher Queue中的任务是一个个DispatcherOperation, 其有一个Name属性,可用来获取是否是自行添加到Dispatcher Queue中的任务,打印出结果诸如这样的形式:
2016/05/11 11:48:00//End] Priority: Background Name: UIH.Mcsf.PA.UtilityCSharp.Commons.CustomCollectionView`1+<>c__DisplayClass4[UIH.Mcsf.PA.UtilityCSharp.Models.StudyInfoModel].<OnInternalListCountChanged>b__0
这可以用来查看Dispatcher Queue的性能问题。
反射获取Name
PropertyInfo pro = typeof(DispatcherOperation).GetProperty("Name", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic);
查看性能问题的示例代码:
Dispatcher.Hooks.OperationPosted += new DispatcherHookEventHandler(Hooks_OperationPosted);
Dispatcher.Hooks.OperationCompleted += new DispatcherHookEventHandler(Hooks_OperationCompleted);
void Hooks_OperationCompleted(object sender, DispatcherHookEventArgs e)
{
var name = pro.GetValue(e.Operation, null);
var str = string.Format("End] Priority: {0} Name: {1} Time:{2}", e.Operation.Priority, name, DateTime.Now);
FileOp.WTF(str);
}
void Hooks_OperationPosted(object sender, DispatcherHookEventArgs e)
{
var name = pro.GetValue(e.Operation, null);
var str = string.Format("Start] Priority: {0} Name: {1} Time:{2}", e.Operation.Priority, name, DateTime.Now);
FileOp.WTF(str);
}