1

In my code, I am using a backgroundworker to do a task and I set that task's return value as the DoWorkEventArgs Result. The strange thing is is that during the DoWork event, the Result is set and contains items, but when I access the result in the RunWorkerCompleted event, SOMETIMES the Result is empty!

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var i =  YouTubeDownloader.GetYouTubeVideoUrls(videoUrls);
            e.Result = i; //Contains items
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
                UseWaitCursor = false;
                if (e.Error != null)
                    throw e.Error;

                List<YouTubeVideoQuality> urls = e.Result as List<YouTubeVideoQuality>; // Empty?
CC Inc
  • 5,842
  • 3
  • 33
  • 64
  • 1
    Probably it is cancelled? Read similar thread [SO C# Background worker setting e.Result in DoWork and getting value back in WorkCompleted](http://stackoverflow.com/questions/856313/c-sharp-background-worker-setting-e-result-in-dowork-and-getting-value-back-in-w) – horgh Jan 28 '13 at 03:08
  • Also is `e.Result` empty? Or couldn't it be cast to `List`? – horgh Jan 28 '13 at 03:15
  • @KonstantinVasilcov Yes, both the array `urls` and `e.Result` is empty. – CC Inc Jan 28 '13 at 03:17
  • 1
    Well, it should be either an exception thrown or `Cancelled` property set to `true` then – horgh Jan 28 '13 at 03:19
  • @KonstantinVasilcov As far as I can tell, neither of those are happening. – CC Inc Jan 28 '13 at 03:31
  • Do you have only one worker running at a time? Could it be possible that you're running several worker threads at a time and some of them return data, while others don't. While debugging you may mix up the one you watched in `DoWork` and the one in `RunWorkerCompleted`... – horgh Jan 28 '13 at 03:35
  • @KonstantinVasilcov That is possible, yes, but unfortunately during runtime with no debugging this still happens, and I need a way to fix it. – CC Inc Jan 28 '13 at 03:36

0 Answers0