Y
Y
Yaroslav Belash2016-08-05 10:02:26
C++ / C#
Yaroslav Belash, 2016-08-05 10:02:26

Why is the program not responding while downloading using the WebClient method?

There is a program, like an updater, the meaning is very simple: the program downloads an archive with files and subsequently unzips it into a designated folder. But why does the program not respond when downloading an archive, but hangs after the download is completed?

string remoteUri = "http://example.xyz/up/";
            string fileName = "new.zip", myStringWebResource = null;
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            myWebClient.DownloadProgressChanged += (s, e) =>
            {
                progressBar.Value = e.ProgressPercentage;
            };
            myWebClient.DownloadFileCompleted += (s, e) =>
            {
                progressBar.Visible = false;
            };

            // Concatenate the domain with the Web resource filename.
            myStringWebResource = remoteUri + fileName;
            // Download the Web resource and save it into the current filesystem folder.
            myWebClient.DownloadFile(myStringWebResource, fileName);
            Complete();
            string usr = SystemInformation.UserName;
            DirectoryInfo di = new DirectoryInfo(path);

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }
            string zipPath = @"new.zip";
            string extractPath = @path;
            ZipFile.ExtractToDirectory(zipPath, extractPath);

            File.Delete("new.zip");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2016-08-05
@Nipheris

Obviously because you are calling the synchronous download method (DownloadFile), and it hangs the event loop until the download is complete. Have you tried downloading asynchronously?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question