Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中从另一个类更新progressbar#_C#_Multithreading_Progress Bar - Fatal编程技术网

C# 在c中从另一个类更新progressbar#

C# 在c中从另一个类更新progressbar#,c#,multithreading,progress-bar,C#,Multithreading,Progress Bar,我在获取进度条以显示下载进度时遇到问题。文件正在下载,没有问题,但有什么东西导致我的progressbar无法更新,我不知道为什么 我尝试过在download和wc\u DownloadProgressChanged方法中手动设置progressBar值,但它实际更改的唯一地方是Form1\u Load方法 using System; using System.Windows.Forms; using System.Threading; namespace Launch { publi

我在获取进度条以显示下载进度时遇到问题。文件正在下载,没有问题,但有什么东西导致我的progressbar无法更新,我不知道为什么

我尝试过在
download
wc\u DownloadProgressChanged
方法中手动设置progressBar值,但它实际更改的唯一地方是
Form1\u Load
方法

using System;
using System.Windows.Forms;
using System.Threading;

namespace Launch
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Downloader downloader = new Downloader();

            ThreadStart job = new ThreadStart(downloader.download);
            Thread thread = new Thread(job);
            thread.Start();

        }

        private void ProgressBar_Click(object sender, EventArgs e)
        {

        }

        public void SetProgress(int val)
        {
            progressBar.Value = val;
        }

        public void SetVisible(bool val)
        {
            progressBar.Visible = val;
        }
    }
}
有什么想法吗?

使用下面的代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        WebClient client = new WebClient();

        client.DownloadProgressChanged += client_DownloadProgressChanged;
        client.DownloadStringCompleted += client_DownloadStringCompleted;

        Uri url = new Uri("url");
        client.DownloadStringAsync(url);


    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        SetVisible(false);
    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        SetProgress(e.ProgressPercentage);
    }
    public void SetProgress(int val)
    {
        progressBar.Value = val;
    }

    public void SetVisible(bool val)
    {
        progressBar.Visible = val;
    }

这很可能属于

首先,不要在循环中添加处理程序

client.DownloadProgressChanged += client_DownloadProgressChanged;
第一次可以,第二次调用2次,第三次调用3次,以此类推。您需要设置一次处理程序。将其移动到第行之后:

WebClient client = new WebClient();
其次,每次触发进度更新时,您都在创建表单的新实例。创建一次私有变量或属性

private Form1 form = new Form1();

编辑: 对于UI,通常只能在创建它的dispatcher线程中修改它或使用封送处理。我只想删除它,因为我们已经下载了字符串async

此外,我不会使用e.ProgressPercentage,而是类似于:

Math.Truncate(e.BytesReceived / (double)e.TotalBytesToReceive * 100)

foreach(dataTable.Rows中的DataRow行)(…)client.DownloadProgressChanged+=client\u DownloadProgressChanged,全部到
var form=new Form1();表格.设定进度(如进度百分比)?我建议使用静态HttpClient和类。它还将防止您的代码(在工作时)为可用连接而与自身发生冲突。现在,每当WebClient实例通知进度时,您都将创建一个新表单(从非UI线程)。你可能会有,可能,几千个。如果代码可以在这些条件下工作。可能比以前更糟。从
Form.Load
事件开始的一个WebClient,应该限制线程池线程中的多个同步下载(错误选择,加载事件会吞噬异常)。顺便说一句,既然这是同步版本,也不会引发
DownloadProgressChanged
事件,为什么还要继续调用
DownloadFile
。测试版本。使用前面提到的
进度
类。此外,所有重叠事件(如果这些事件是生成的)都应该使用单个ProgressBar通知进度。如果您发现基于任务的方法很难实现,那么选择事件驱动的方法。这将引发
DonwloadProgress
downloadpompled
事件(您还需要订阅后者)。您必须处理多个进度事件并更新多个进度条。您还需要处理创建的每个WebClient实例。OP正在尝试同时从多个不同的远程资源下载多个文件,可能是从多个不同的远程资源下载多个文件。这只是设置了一个下载。顺便说一句,您应该在调用
DownloadStringAsync
之前连接好处理程序。它可以在方法中使用并单独调用。但是,一个进度条不能处理多个下载。只需获取多个下载进度值并取平均值,然后显示在进度条中。这就是为什么OP尝试创建一个带有进度条的新表单来报告每次下载的进度。顺便说一句,OP使用的是
DownloadFile
,而不是
DownloadString
(或
DownloadStringAsync
)。OP还应验证
DownloadFile
是否实际引发了DownloadProgress/DownloadComplete事件。不幸的是,此示例无法实现我试图实现的目的,即在progressbar中设置另一个类的下载进度。您好Margus,我已经按照您的建议更改了代码,但进度条似乎仍然没有更改。关于什么可能是错误的还有其他想法吗?谢谢。嗨,对不起,我不确定我是否理解,我不是异步下载的,我想你指的是其他人的答案。不在同一个线程中是有意义的,你能给我一个封送的例子吗?@Zac1989是的,与Saeiddjawadi混合。一个例子,谢谢你,这给了我一些想法。请看我的编辑在我原来的帖子,我觉得我非常接近一个工作的解决方案,如果你有任何进一步的见解。
private Form1 form = new Form1();
Math.Truncate(e.BytesReceived / (double)e.TotalBytesToReceive * 100)