【问题标题】:Update two progressbar in same time同时更新两个进度条
【发布时间】:2013-08-07 11:14:47
【问题描述】:

我有来自 ftp 服务器的自动上传应用程序和两个进度条来更新整体和当前下载状态。 第一个工作正常(update% = currentFile as int / allFilesToDownload *100%)。但我想上传当前文件下载。 我的代码:

Uri url = new Uri(sUrlToDnldFile);

                    int inde = files.ToList().IndexOf(file);
                    string subPath = ...
                    bool IsExists = System.IO.Directory.Exists(subPath);
                    if (!IsExists)
                        System.IO.Directory.CreateDirectory(subPath);
                    sFileSavePath = ...
                    System.Net.FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(file));

                    System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();
                    response.Close();
                    long iSize = response.ContentLength;
                    long iRunningByteTotal = 0;

                    WebClient client = new WebClient();

                    Stream strRemote = client.OpenRead(url);

                    FileStream strLocal = new FileStream(sFileSavePath, FileMode.Create, FileAccess.Write, FileShare.None);

                    int iByteSize = 0;

                    byte[] byteBuffer = new byte[1024];

                    while ((iByteSize = strRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
                    {
                        strLocal.Write(byteBuffer, 0, iByteSize);
                        iRunningByteTotal += iByteSize;
//THERE I'D LIKE TO UPLOAD CURRENT FILE DOWNLOAD STATUS
                        string a = iByteSize.ToString();
                        double b = double.Parse(a.ToString()) / 100;
                        string[] c = b.ToString().Split(',');
                        int d = int.Parse(c[0].ToString());
                        update(d);
//update(int prog) {  bgWorker2.ReportProgress(prog); }


                    }

                    double dIndex = (double)(iRunningByteTotal);
                    double dTotal = (double)iSize;
// THIS CODE COUNTING OVERAL PROGRESS - WORKS FINE
                    double iProgressPercentage1 = double.Parse(ind.ToString()) / double.Parse(files.Count().ToString()) * 100;
                    ind++;
                    string[] tab = iProgressPercentage1.ToString().Split(',');

                    int iProgressPercentage = int.Parse(tab[0]);
                    currentFile = file;
                    bgWorker1.ReportProgress(iProgressPercentage);
                    strRemote.Close();

不幸的是我仍然收到错误,我无法更新progressBar2,因为另一个进程正在使用它。 有什么办法吗? 谢谢

【问题讨论】:

  • 进程?你的意思是thread,对吧?

标签: c# wpf progress-bar backgroundworker


【解决方案1】:

通过 dispatcher.BeginInvoke 方法更新值。

Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(()=>
{
  progressbar2.value = newvalue;
}));

这个Dispatcher 将把你的工作推到持有Progressbar2 的主线程。

【讨论】:

    猜你喜欢
    • 2013-03-08
    • 2020-11-05
    • 1970-01-01
    • 2014-04-09
    • 2017-07-05
    • 2014-07-31
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多