WPF Dispatcher Invoke return value is always null

青春壹個敷衍的年華 提交于 2019-12-04 06:25:31

D'oh, here's how to do what you are trying to do:

object retVal;
slide.Dispatcher.Invoke(new Action(() => retval = imageService.GenerateProxyImage(slide)));

Edit: The ThreadStart threw me off - this isn't multithreaded. What are you trying to accomplish with this code sample??

Zenuka

It's because ThreadStart doesn't have a return type (void()).

Try this instead:

UIElement retVal = slide.Dispatcher.Invoke(new Func<UIElement>( () => imageService.GenerateProxyImage(slide))); 
Lee

The 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 the ThreadStart delegate you are using is void, you need to use a Func<T> or a custom delegate with a return value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!