wlalth 0 Newbie Poster

Hi, i wrote a patch system. It check the links and download them, if the files not same with the users files.

All steps are working, i have only one problem. First download is perfectly complated, but when DownloadFileAsycn fired second times, its don't work. It creates strange file of 0 bytes, with name of second file.

Im posting a part of my codes.

progressBar1.Value = 0;
            string netdboyu = "0";
            string userdboyu = "0";
            string webdizin = "my url is here";
            


            WebClient indir = new WebClient();
            indir.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            indir.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
indir.DownloadFileAsync(new Uri(webdizin + filename), (filename));// filename is parameter of function.

These codes are under a function. When download process complated im calling it again, im using switch function before called. With that, my update function is trying to download the correct file. But can't download.

Dani AI

Generated

Hi — a common cause of “first async download OK, second one produces a 0‑byte file” is the WebClient instance not staying alive for the entire async operation. If you create the client in a short‑lived local method variable (and don’t hold a reference), the GC or premature disposal can abort the background I/O. Make sure the client stays reachable until the DownloadFileCompleted event finishes, then dispose and null it. Also check the Completed args for errors and cancelled status so you know whether the download actually failed.

Practical checklist to try now:

  • Keep each WebClient instance as a field (or in a list) while the async download runs; only Dispose() it inside the Completed handler.
  • In DownloadFileCompleted inspect e.Error and e.Cancelled and log the message to see the real failure.
  • Start the next file only after the Completed handler confirms success.
  • As a quick test, try a synchronous download for that second URL to rule out server side issues.
  • If you can use newer frameworks, consider the Task API (DownloadFileTaskAsync) to simplify sequential downloads.

A minimal Completed handler should check e.Error/e.Cancelled, unsubscribe handlers, Dispose the client, and then kick off the next download. For reference see the official docs: WebClient.DownloadFileAsync, DownloadFileTaskAsync, and the .NET garbage collection basics: Garbage collection fundamentals.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.