WPF Dispatcher Invoke return value is always null

Mrroper picture Mrroper · Sep 8, 2009 · Viewed 8k times · Source

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;
}

Answer

Zenuka picture Zenuka · Sep 8, 2009

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)));