0

我有一个严重的问题。我目前正在做一个补丁程序。
程序中有一个“补丁”按钮,如果用户点击它,下载就开始了。
目前该程序需要下载 5 个文件。下载是正确的,这部分程序正在运行,但是当我点击补丁按钮时,程序开始滞后,我无法关闭,或者改变它的位置。这是代码:

private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(new ThreadStart(this.download)); 线程.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();

文件.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";
}

我该如何解决滞后问题?(正如我之前所说,如果我点击“补丁”按钮,文件正在下载,但程序“停止”)

4

2 回答 2

1

要停止应用程序的下载部分以阻止应用程序的 UI 部分,为什么不在不同的线程上执行下载工作。这样,即使下载尚未完成,您的 UI 仍会响应。

编辑:我认为这是一篇适用于您的案例的好文章:

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

于 2013-02-22T14:19:42.700 回答
0

我想你误会了InvokeRequired

来自MSDN

获取一个值,该值指示调用者在对控件进行方法调用时是否必须调用调用方法,因为调用者位于与创建控件的线程不同的线程上。

基本上,您在另一个线程上开始下载。但是由于这将导致InvokeRequired为真,您可以回忆一下 GUI 线程上的方法,锁定应用程序。

如果删除此部分会更好吗?

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

更新 要排除您在进度条上做错了什么,请尝试将您的下载方法简化为:

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");
}

应该使文件下载没有延迟,但您希望在进度栏中看到任何更新(显然)。

于 2013-02-22T14:25:38.790 回答