显示进度条,直到从服务器C#收到数据

时间:2018-03-28 05:52:41

标签: c# winforms progress-bar

我有一个小工具,它获取文件大小和URL的名称,但是当我运行代码并输入文件URL需要时间时,用户看起来就像是不工作。

我希望在收到数据之前显示进度条,以便用户可能不会认为该应用程序无效。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Multi_Tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("You have not typed the URL", "URL Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    string URL = textBox1.Text;
                    string filetype = URL.Substring(URL.LastIndexOf(".") + 1,
                            (URL.Length - URL.LastIndexOf(".") - 1));
                    FileType.Text = filetype.ToUpper();
                    string filename = URL.Substring(URL.LastIndexOf("/") + 1,
                            (URL.Length - URL.LastIndexOf("/") - 1));
                    namelabel.Text = filename;
                    System.Net.WebRequest req = System.Net.HttpWebRequest.Create(textBox1.Text);
                    req.Method = "HEAD";
                    System.Net.WebResponse resp = req.GetResponse();
                    long ContentLength = 0;
                    long result;
                    if (long.TryParse(resp.Headers.Get("Content-Length"), out ContentLength))
                    {
                        string File_Size;

                        if (ContentLength >= 1073741824)
                        {
                            result = ContentLength / 1073741824;
                            kbmbgb.Text = "GB";
                        }
                        else if (ContentLength >= 1048576)
                        {
                            result = ContentLength / 1048576;
                            kbmbgb.Text = "MB";
                        }
                        else
                        {
                            result = ContentLength / 1024;
                            kbmbgb.Text = "KB";
                        }
                        File_Size = result.ToString("0.00");
                        sizevaluelabel.Text = File_Size;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Clear(); 
        }  
    }
}

enter image description here

2 个答案:

答案 0 :(得分:2)

  

您可以使用后台工作程序将下载移动到另一个线程并显示进度条,直到收到数据。

ProgressForm是一个包含进度条的表单,您可以在下载数据之前显示该进度条

this.progressBar1 = new System.Windows.Forms.ProgressBar();
// 
// progressBar1
// 
this.progressBar1.Location = new System.Drawing.Point(12, 30);
this.progressBar1.MarqueeAnimationSpeed = 1;
this.progressBar1.Maximum = 2500;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(522, 23);
this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
this.progressBar1.TabIndex = 1;


//constructor
public frmProgress(string text)
{
   this.Text = text;
   InitializeComponent();
}
  

如果您想在进度条中显示值,请确保将properties更改回正常(截至目前已将其设置为marquee)。但正如您所说的那样只需4秒就可以使用marquee

//Method to increment value of progress bar
public void PrgBarInc()
{
  if (this.IsHandleCreated)
  {
    if (this.InvokeRequired)
    {
       this.Invoke(new MethodInvoker(PrgBarInc));
    }
    else
    {
       prgBar.Increment("your val");
    }
}

==============================主UI类============== ================

BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);

//Add In your method which initiates download 
public void PerformDownload()
{
  ProgressForm = new frmProgress("your text");
  ProgressForm.ShowInTaskbar = false;
  backgroundWorker1.RunWorkerAsync();
  ProgressForm.ShowDialog();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   //perform service request
   //if any of your task gets compeleted just call
   //ProgressForm.PrgBarInc()
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    ProgressForm.Close();
    ProgressForm.Dispose();
}

答案 1 :(得分:0)

通常,您可以像这样更新进度条:

conda install gfortran_linux-64

在BeginInvoke()中包装它会使UI响应:

progressBar1.Value = N;