none
关于WPF进度条的问题 RRS feed

  • 问题

  • 目前打算使用wpf做一个简单的FTP工具,现在无法控制进度条控件的进度显示。目前我的测试代码如下,我觉得问题就是出在Download函数中的

    w.ReportProgress(Convert.ToInt32(temp/proces)*100);这句,不知道单步跳的时候可以看到Convert.ToInt32(temp/proces)*100的值是从1到100的,但是前端UI就是没有反应.

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                worker = new BackgroundWorker();
                worker.WorkerReportsProgress = true;
                worker.WorkerSupportsCancellation = true;
                worker.DoWork += new DoWorkEventHandler(worker_DoWork);
                worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
                worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
                progressBar1.Value = 0;
                textBlock1.Text = "downloading...";
                worker.RunWorkerAsync();
            }

    void worker_DoWork(object sender, DoWorkEventArgs e)
            {
                e.Result = Download("c:/MaxDos8.exe", sender, e);
            }

     void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar1.Value = e.ProgressPercentage;
            }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                Action<string> act = delegate(string str)
                {
                    textBlock1.Text = str;
                };
                if (e.Cancelled)
                    this.Dispatcher.BeginInvoke(act, "download cancelled.");
                else
                    this.Dispatcher.BeginInvoke(act, e.Result.ToString());
            }

    private static string Download(string name, object sender, DoWorkEventArgs e)
            {

                FileInfo fileInf = new FileInfo(name);
                string uri = "ftp://" + "210.51.*.**" + "/" + fileInf.Name;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri
                ("ftp://" + "210.51.*.**" + "/" + fileInf.Name));
                reqFTP.Credentials = new NetworkCredential("***", "*******");
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.ContentLength = fileInf.Length;
                int buffLength = 2048;
                long proces = fileInf.Length /2048;
                byte[] buff = new byte[buffLength];
                int contentLen;
                FileStream fs = fileInf.OpenRead();
                try
                {
                    // 把上传的文件写入流 
                    Stream strm = reqFTP.GetRequestStream();

                    // 每次读文件流的2kb 
                    contentLen = fs.Read(buff, 0, buffLength);
                    long temp = 1;
                    // 流内容没有结束 
                    while (contentLen != 0)
                    {
                        // 把内容从file stream 写入 upload stream  
                        w.ReportProgress(Convert.ToInt32(temp/proces)*100);
                        System.Threading.Thread.Sleep(50);
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                        temp++;
                    }

                    // 关闭两个流 
                    strm.Close();
                    fs.Close();


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Upload Error");
                } 
                return "download completed, download " + DateTime.Now.Millisecond + "byte";
            }

           

    2011年4月13日 3:13

答案

  • .........果然还是w.ReportProgress(Convert.ToInt32(temp/proces)*100);

    应该写成w.ReportProgress(Convert.ToInt32(temp*100/proces));就好了

    因为temp和proces都是long类型了,除完以后小于1的话就是得0了...问题已经解决了...还是经验不足啊

    • 已标记为答案 yooho 2011年4月13日 3:28
    2011年4月13日 3:28