Domanda

I have a Winform application. After button2 is clicked, the application will start to download a lot of images with url stored in list of ThousandsOfImageURLs. The application works fine for the first 40 images download. But after that, RunBrowserThread can not be started by Task.Factory.StartNew in function ParallelRunBrowser. I can see every 10 seconds Task.Factory.StartNew is hit but the the breakpoint inside RunBrowserThread of class DownLoadImages can not be hit any more after about 40 images are downloaded. I don't see any exception was thrown in a few hours hanging in this way.

What is the possible issue?

Thanks,

    private void button2_Click(object sender, EventArgs e)
    {
        var sta = new StaTaskScheduler(numberOfThreads: 4);
        var doImages = new ThreadLocal<DownLoadImages>(() => new DownLoadImages());

        foreach (string url in ThousandsOfImageURLs)
              ParallelRunBrowser(strSiteRoot + url, sta, doImages);
            }
        }
    }

    private void ParallelRunBrowser(string url, StaTaskScheduler sta, ThreadLocal<DownLoadImages> doImages)
    {
        Thread.Sleep(10000);

            Task.Factory.StartNew(() =>
            {
                doImages.Value.RunBrowserThread(new Uri(url));
            },
            CancellationToken.None,
            TaskCreationOptions.None,
            sta);
    }


class DownLoadImages
{
    public void RunBrowserThread(Uri url)
    {
            var br = new WebBrowser();
            br.DocumentCompleted += Browser_DocumentCompleted;
            br.Navigate(url);
            Application.Run();
    }

    void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var br = sender as WebBrowser;
        if (br.Url == e.Url)
        {
                FinishDownLoadHere(sender, e);
                Application.ExitThread();   
                ((WebBrowser)sender).Dispose();
                GC.Collect();
        }
    } 
}
È stato utile?

Soluzione

Using webbrowsers, Application.Run and GC.collect seems a bit much.

Why not use a WebClient to get the images?

From: Download image from the site in .NET/C#

string localFilename = @"c:\localpath\tofile.jpg";
using(WebClient client = new WebClient())
{
    client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top