WPF 调度程序调用返回值始终为 null
我调用了一个返回 UIElement
的方法,我使用 Dispatcher
调用该方法,下面是代码。
然而,Dispatcher 调用的返回值始终为 NULL,有什么想法吗?
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var slides = (IList<UIElement>)e.Argument;
var bmpSlides = new List<UIElement>();
var imageService = new ImageService();
int count = 0;
foreach (UIElement slide in slides)
{
object retVal = slide.Dispatcher.Invoke(
new ThreadStart(() => imageService.GenerateProxyImage(slide)));
bmpSlides.Add(imageService.GenerateProxyImage(slide));
_backgroundWorker.ReportProgress(count / 100 * slides.Count);
count++;
}
e.Result = bmpSlides;
}
I have a call to a method that returns a UIElement
that I call using the Dispatcher
, below is the code.
However the return value of the Dispatcher
invoke is always NULL, any ideas?
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var slides = (IList<UIElement>)e.Argument;
var bmpSlides = new List<UIElement>();
var imageService = new ImageService();
int count = 0;
foreach (UIElement slide in slides)
{
object retVal = slide.Dispatcher.Invoke(
new ThreadStart(() => imageService.GenerateProxyImage(slide)));
bmpSlides.Add(imageService.GenerateProxyImage(slide));
_backgroundWorker.ReportProgress(count / 100 * slides.Count);
count++;
}
e.Result = bmpSlides;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哦,这是如何做你想做的事情:
编辑: ThreadStart 让我失望 - 这不是多线程的。您想通过此代码示例实现什么目的?
D'oh, here's how to do what you are trying to do:
Edit: The ThreadStart threw me off - this isn't multithreaded. What are you trying to accomplish with this code sample??
这是因为
ThreadStart
没有返回类型 (void()
)。试试这个:
It's because
ThreadStart
doesn't have a return type (void()
).Try this instead:
Dispatcher.Invoke
的 文档 指出返回值是“正在调用的委托的返回值,或者如果委托没有返回值,则返回空引用(在 Visual Basic 中为 Nothing)”。由于您使用的 ThreadStart 委托无效,因此您需要使用 FuncThe documentation for
Dispatcher.Invoke
states the return value is "The return value from the delegate being invoked or a null reference (Nothing in Visual Basic) if the delegate has no return value." Since theThreadStart
delegate you are using is void, you need to use aFunc<T>
or a custom delegate with a return value.