0
votes

I have a serious problem. I'm currently doing a patcher program.
There's a "Patch" button in the program, and if the user click it, the download is starting.
There are currently 5 files that the program needs to download. The downloading is correct, this part of the program is working but when I click the Patch button, the program starts lagging, and I can't close, or change the position of it. Here's the code:

private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(this.download)); thread.Start(); }

    public void download()
    {
        int downloaded = 0;
        int all = 5;
        WebClient myWebClient = new WebClient();

        if (button1.InvokeRequired)
        {
            MethodInvoker MethodControll = new MethodInvoker(download);
            this.button1.Invoke(MethodControll);
        }
        else
        {

            double state;
            jelenlegidownload.Text = "alut.dll";
            myWebClient.DownloadFile(Files.alutDLL, "alut.dll");
            downloaded++;
            state = downloaded / all * 100;
            progressBar.Value = Convert.ToInt32(state);
            progressBar.Refresh();
            this.Refresh();  

            jelenlegidownload.Text = "BlackBox.dll";
            myWebClient.DownloadFile(Files.BlackBoxDLL, "BlackBox.dll");
            downloaded++;
            state = downloaded / all * 100;
            progressBar.Value = Convert.ToInt32(state);
            progressBar.Refresh();
            this.Refresh();

            jelenlegidownload.Text = "DevIL.dll";
            myWebClient.DownloadFile(Files.DevILDLL, "DevIL.dll");
            downloaded++;
            state = downloaded / all * 100;
            progressBar.Value = Convert.ToInt32(state);
            progressBar.Refresh();
            this.Refresh();

            jelenlegidownload.Text = "fltkdll.dll";
            myWebClient.DownloadFile(Files.fltkdllDLL, "fltkdll.dll");
            downloaded++;
            state = downloaded / all * 100;
            progressBar.Value = Convert.ToInt32(state);
            progressBar.Refresh();
            this.Refresh();

            jelenlegidownload.Text = "glut32.dll";
            myWebClient.DownloadFile(Files.glut32DLL, "glut32.dll");
            downloaded++;
            state = downloaded / all * 100;
            progressBar.Value = Convert.ToInt32(state);
            progressBar.Refresh();
            this.Refresh();

The Files.cs:

public class Files
{
    public static string alutDLL = "https://dl.dropbox.com/s/62tt9w194xefk7t/alut.dll?token_hash=AAHQmybYdR44TRrS9bWQWV7jlZBzZQ-mmmjNy1Kv_qR4cg&dl=1";
    public static string BlackBoxDLL = "https://dl.dropbox.com/s/vtdrl8qdpky8p08/BlackBox.dll?token_hash=AAHCtQPBJ5s-3aL5B4FqrmOUIGP6BVvW8ZQeWd-xBzysTw&dl=1";
    public static string DevILDLL = "https://dl.dropbox.com/s/spni307vmk4zng9/DevIL.dll?token_hash=AAEmZdQj3dv2NIEh6tcWwkgyJHCytSsX65QXZyNGY2Vl1w&dl=1";
    public static string fltkdllDLL = "https://dl.dropbox.com/s/fsa29pelfwgk5ha/fltkdll.dll?token_hash=AAF55SuU_8bfli5gIiPpA-VLWUmZKLbOK-Ys8iokuJ8_XA&dl=1";
    public static string glut32DLL = "https://dl.dropbox.com/s/cptiwxv17nhtywp/glut32.dll?token_hash=AAGCNXQPpwrByjp-uG_avBbkNyNjTfOJFxbY3ieNAfLzVw&dl=1";
}

How can I fix the lagging? (As I said before, if I click the "Patch" button, the files are downloading, but the program "stops")

2

2 Answers

1
votes

To stop the download part of your application to block your UI part of the application, why don't you do the download job on a different thread. This way your UI will still be responsive even if the download has not been finished.

Edit: I think this is a great article that applies to your case:

http://www.developerfusion.com/article/4134/net-threading-part-i/

0
votes

I think you have misunderstood InvokeRequired.

From MSDN:

Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on.

Basically, you start your download on another thread. But since this will cause InvokeRequired to be true, you recall the method on the GUI thread, locking the application.

Does it work better if you remove this section?

    if (button1.InvokeRequired)
    {
        MethodInvoker MethodControll = new MethodInvoker(download);
        this.button1.Invoke(MethodControll);
    }

Update To rule out that your doing something wrong with your progress bar, try simplify your download method to this:

public void download()
{
    WebClient myWebClient = new WebClient();

    myWebClient.DownloadFile(Files.alutDLL, "alut.dll");
    myWebClient.DownloadFile(Files.BlackBoxDLL, "BlackBox.dll");
    myWebClient.DownloadFile(Files.DevILDLL, "DevIL.dll");
    myWebClient.DownloadFile(Files.fltkdllDLL, "fltkdll.dll");
    myWebClient.DownloadFile(Files.glut32DLL, "glut32.dll");
}

This should make the files downloaded without lag, but you want see any updates in your progress bar (obviously).