0
 private void Download_Click(object sender, EventArgs e)
        {
            label1.Text = "Downloading...";
            WebClient wc = new WebClient();
            string program = "Program";
            string link = "https://linkhere.com";
            string download = wc.DownloadString(link);
            string path = "Program\\" + program + ".zip";
            string patch = "Program";
            Directory.CreateDirectory(patch);
            wc.DownloadFile(download, path);
            label1.Text = "Downloaded!";
        }

I want to make the label1.Text = "Downloaded!"; to happen after it downloads the program.

chili
  • 1
  • 2
  • 2
    _"I want to make the label1.Text = "Downloaded!"; to happen after it downloads the program."_ - thats exactly what should happen as you've chosen the synchronous version of that download method. – Jamiec Jan 06 '22 at 11:37
  • welcome to StackOverflow. Please edit you post and try to be more precise on the problem: what is the exact difference between the expected and actual outcome of your code. Up to now we don't really see the problem – Mong Zhu Jan 06 '22 at 11:40

4 Answers4

2

You need to make the download asynchronous to prevent the deadlock.

private async void Download_Click(object sender, EventArgs e)
{
    label1.Text = "Downloading...";
    WebClient wc = new WebClient();
    string program = "Program";
    string link = "https://linkhere.com";
    string download = wc.DownloadString(link);
    string path = "Program\\" + program + ".zip";
    string patch = "Program";
    Directory.CreateDirectory(patch);
    await wc.DownloadFileAsync(download, path);
    label1.Text = "Downloaded!";
}
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
  • 2
    `DownloadFileAsync` != `DownloadFileTaskAsync` (it's not awaitable) -- WebClient really needs to be disposed of. – Jimi Jan 06 '22 at 19:35
  • @Jimi it's pretty standard practice to have an instance of WebClient running for the lifetime of the class. What version of .net are you on? – Yuriy Faktorovich Jan 06 '22 at 20:02
  • 1
    That's a local object. -- The .Net version is not factor. – Jimi Jan 07 '22 at 00:15
  • 2
    The most important thing is that `DownloadFileAsync` is not awaitable, it's event-driven. – Jimi Jan 07 '22 at 00:31
  • @Jimi you're doing something wrong, it is awaitable – Yuriy Faktorovich Jan 07 '22 at 02:42
  • 2
    `public void DownloadFileAsync (Uri address, string fileName);` -- declared `void`: how do you await `void`? Doesn't matter, it's not awaitable, as mentioned it's event-driven. The awaitable method is the `DownloadFileTaskAsync` counterpart (which of course returns a `Task`). – Jimi Jan 07 '22 at 06:03
  • 1
    @dr.null It's not about awaiting an async call in an `async void` method, it's DownloadFileAsync that is not awaitable. It's an [AsyncOperation](https://referencesource.microsoft.com/#System/compmod/system/componentmodel/AsyncOperation.cs,e6f378e28a257e11), i.e., it's *asynchronous* in the *old* terms, driven by events, it cannot be handled as a Task, simply because it's not. – Jimi Jan 07 '22 at 15:42
  • @Jimi aha, got it. Thank you Jim. – dr.null Jan 07 '22 at 15:54
0

The DownloadFile docs say "This method blocks while downloading the resource", so I'm not sure what could be different about your situation, or if there even is a problem.

I suspect that you never see "Downloading..." because the UI does not update in between the two label1.Text update calls. Adding Application.DoEvents() after the first .Text update may help.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • 1
    As for the `Application.DoEvents`, please read [this](https://stackoverflow.com/a/5183623/14171304). – dr.null Jan 06 '22 at 12:13
0

Please Use tasks and events to handle the situation.

Pran
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 06 '22 at 12:06
0

You can modify your download to with this as reference.

    public static async Task Main()
    {
        Task task1 = new Task(() => ActionToWork1());
        Task task2 = new Task(() => ActionToWork2());
        
        task1.Start();
        task2.Start();
        await Task.WhenAll(task1, task2);
        Console.WriteLine("All task done.");    
    }
    
    private static void ActionToWork1(){
        Console.WriteLine("Working on Task 1"); 
    }
    
    private static void ActionToWork2(){
        Console.WriteLine("Working on Task 2"); 
    }
Kung Fu Panda
  • 636
  • 10
  • 22