0
public void Imagesaver(string url)
{
    string result = Filename(url);
    string SourceCode = worker.GetSourceCode(url);

    List<string> names1 = new List<string>();

    MatchCollection data2 = Regex.Matches(SourceCode, "(src=\"|src=\\/\")([^>]*?jpg|png|gif)", RegexOptions.Singleline);

    string name = string.Empty;

    foreach (Match m in data2)
    {
        name = m.Groups[2].Value;

        if (name.Contains("http"))
        {
            names1.Add(name);
        }
        else
        {
            names1.Add(url+name);
        }
    }

    WebClient client = new WebClient();
    int i = 0;

    foreach (string name2 in names1)
    {
        Uri imageurl = new Uri(name2);

        try
        {
            client.DownloadFileAsync(imageurl, (@"C:\Users\Ramazan BIYIK\Desktop\HTML içerik\" + result+"\\" + i + ".jpg"));
            i++;
        }
        catch(Exception ex) 
        {
            continue; 
        }
    }
}

I have a code block that saves .jpg, .png and .gif extension files from source code in a website to local. But when I tried in debug and non-debug mode, it showed me different results.

For example in a web site there are 40 files with a .jpg extension. In debug mode, I can download them, all but at runtime, I can download only 6 files.

I searched that problem but all answers are about numbers and I am not dealing with number (I suppose). Please help and sorry for my bad English.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rmznbyk 1
  • 1,104
  • 2
  • 13
  • 27
  • 1
    My guess is that since you don't `await` on `client.DownloadFileAsync()`, multiple requests are issued concurrently, which is not supported by WebClient ([see this](https://stackoverflow.com/questions/13755623/webclient-does-not-support-concurrent-i-o-operations)), but these exceptions are never caught because results of tasks returned by `client.DownloadFileAsync()` are not observed. In another words, make method `async` and `await` on `client.DownloadFileAsync()`; – Ňuf Oct 14 '17 at 20:53
  • if you are getting different results with release than debug then the release was not fully compiled with latest source. Usually deleting bin folder in project will force all the code to recompile. You can check the time stamp of the exe file in the release and debug folder and make sure the release executable is later then the debug. – jdweng Oct 14 '17 at 21:34

1 Answers1

0

The problem is solved thanks to -Nuf.Please share your commend as answer. I followed this link. The problem was that, I was trying only one webclient instance.Here is the answer for my problem.

foreach (string name2 in names1)
{
       WebClient client = new WebClient();
       Uri imageurl = new Uri(name2);
       try
       {

           client.DownloadFileAsync(imageurl, (@"C:\Users\Ramazan BIYIK\Desktop\HTML içerik\" + result+"\\" + i + ".jpg"));
           i++;
        }
           catch(Exception ex) {continue; }
}

Hovewer using async method and await expression on some cases can work(Or I'am using it in a wrong way):

public async void Imagesaver(string url)
{

    string result = Filename(url);
    string SourceCode = worker.GetSourceCode(url);
    List<string> names1 = new List<string>();
    MatchCollection data2 = Regex.Matches(SourceCode, "(src=\"|src=\\/\")([^>]*?jpg|png|gif)", RegexOptions.Singleline);

    string name = string.Empty;

        foreach (Match m in data2)
        {

            name = m.Groups[2].Value;

            if (name.Contains("http"))
            {
                names1.Add(name);
            }
            else
            {
                names1.Add(url+name);
            }

        }

        int i = 0;
        WebClient client = new WebClient();
        foreach (string name2 in names1)
        {

            Uri imageurl = new Uri(name2);
            try
            {

              await  Task.Run(()=>client.DownloadFileAsync(imageurl, (@"C:\Users\Ramazan BIYIK\Desktop\HTML içerik\" + result+"\\" + i + ".jpg")));
                i++;
            }
            catch(Exception ex) {continue; }
        }

With this code block ı can download more file then first code block(The code block in my question).

As a result ı could download only 6 files with only one instance of webclient.(with the code block which is in my question). When ı use async method, ı could download 18 files. But when ı use instance in foreach loop, ı could download all files(40 files).

rmznbyk 1
  • 1,104
  • 2
  • 13
  • 27