Ty Kohler
Ty Kohler

Reputation: 3

Stopping application from not displaying because of form_load code

How can I stop my application from not displaying until the form_load code is completed?

public partial class updater : Form
{
    public updater()
    {           
        InitializeComponent();
        timer1.Interval = (10000) * (1);
        progressBar1.Value = 0;
        progressBar1.Maximum = 100;
        progressBar1.Update();
        timer1.Start();
    }

    private void updater_Load(object sender, EventArgs e)
    {          
        WebClient webClient = new WebClient();
        webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;

        webClient.DownloadFile("http://download827.mediafire.com/jl9c098fnedg/ncqun56uddq0y1d/Stephen+Swartz+-+Survivor+%28Feat+Chloe+Angelides%29.wav", Application.StartupPath + "\\Stephen Swartz - Survivor (Feat Chloe Angelides).wav");
        // System.Diagnostics.Process.Start("\\Test.exe");
        this.Close();   
    }
    void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        progressBar1.Update();
    }
}

Upvotes: 0

Views: 75

Answers (2)

Dmitrii Dovgopolyi
Dmitrii Dovgopolyi

Reputation: 6301

One way is to move your code from Load Shown Event. So the code will start runing after the form is shown.

The other is create a thread, where you will download a file. For this purpose you can use BackgroundWorker

private void updater_Load(object sender, EventArgs e)
{     
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += (s, eArgs) =>
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFile("someUrl", "somePath");
        };
    worker.RunWorkerAsync();
}

Also there exists webClient.DownloadFileAsync method witch suits better in this situation. You can find description in sa_ddam213 answer.

Upvotes: 1

sa_ddam213
sa_ddam213

Reputation: 43596

If you use DownloadFileAsync it wont block the UI thread and will allow the Form to load and show the progress in the Progressbar, then you can use the DownloadFileCompleted event to close the Form

Example:

    public Form1()
    {
        InitializeComponent();
        progressBar1.Value = 0;
        progressBar1.Maximum = 100;
        progressBar1.Update();
    }

    private void updater_Load(object sender, EventArgs e)
    {
        WebClient webClient = new WebClient();
        webClient.DownloadProgressChanged += webClient_DownloadProgressChanged;
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
        webClient.DownloadFileAsync(new Uri("http://download827.mediafire.com/jl9c098fnedg/ncqun56uddq0y1d/Stephen+Swartz+-+Survivor+%28Feat+Chloe+Angelides%29.wav"), Application.StartupPath + "\\Stephen Swartz - Survivor (Feat Chloe Angelides).wav");
    }

    private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        Close();
    }

    private void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        progressBar1.Update();
    }

Upvotes: 3

Related Questions