调度程序XPS内存泄漏 [英] Dispatcher xps memory leak
问题描述
我使用com interop从vb6应用程序中调用.net 4.0 dll.在.net中,我通过xaml固定文档创建了xps文档并将其保存到磁盘.这会导致内存泄漏,我在这里找到了一个很好的解决方案.
I'm call a .net 4.0 dll from a vb6 app using com interop. In .net I create an xps document, via a xaml fixed document and save it to disk. This causes and memory leak and I've found a great solution here.
将FixedDocument保存到XPS文件会导致内存不足泄漏
上面适用于我的解决方案涉及以下代码行:
The solution above, that worked for me, involves this line of code:
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle, new DispatcherOperationCallback(delegate { return null; }), null);
这一行代码到底发生了什么.通过将委托设置为null是否可以处理Dispatcher对象?
What exactly is happening with this line of code. Is that by setting the delegate to null this disposes the Dispatcher object?
推荐答案
虽然最初看起来所提供的代码不执行任何操作,但实际上具有非显而易见的副作用,可以解决您的问题.让我们将其分解为几个步骤:
While it initially appears that the supplied code does nothing, it actually has a non-obvious side effect that is resolving your problem. Let's break it down into steps:
-
Dispatcher.CurrentDispatcher
获取当前线程的调度程序. -
Invoke
在调度程序的线程(当前线程)上同步执行提供的委托 -
DispatcherPriority.SystemIdle
设置执行优先级 -
new DispatcherOperationCallback(delegate { return null; })
创建一个不执行任何操作的委托 -
null
作为参数传递给委托人
Dispatcher.CurrentDispatcher
Gets the dispatcher for the current thread.Invoke
synchronously executes the supplied delegate on the dispatcher's thread (the current one)DispatcherPriority.SystemIdle
sets the execution prioritynew DispatcherOperationCallback(delegate { return null; })
creates a delegate that does nothingnull
is passed as an argument to the delegate
总的来说,这看起来什么也没做,实际上它实际上什么也不做.重要的是它要等到当前线程的调度程序清除了所有优先级比SystemIdle
高的计划任务,然后再执行无"操作.这样,可以在您返回vb6应用之前执行计划的清理工作.
All together, this looks like it does nothing, and indeed it actually does do "nothing". The important part is that it waits until the dispatcher for the current thread has cleared any scheduled tasks that are higher priority than SystemIdle
before doing the "nothing". This allows the scheduled cleanup work to happen before you return to your vb6 app.
这篇关于调度程序XPS内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!