繁体   English   中英

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

[英]Showing Progress Bar until the data is received from the server C#

我有一个小型工具,可以获取文件大小和URL名称,但是运行代码并输入文件URL会花费一些时间(大约4秒),这对于用户来说是很费时间的,看起来好像无法正常工作。

我想显示一个进度条,直到接收到数据,以便用户可能不会认为该应用程序无法正常工作。

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

在此处输入图片说明

您可以使用后台工作者将下载的内容移至另一个线程并显示进度条,直到接收到数据为止。

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

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

progressBar1.Value = N;

将其包装在BeginInvoke()中可使UI响应:

Dispatcher.BeginInvoke(DispatcherPriority.Normal,
    new DispatcherOperationCallback(delegate
               {
                   progressBar1.Value = progressBar1.Value + 1;
                   //update in UI Thread
                   return null;
               }), null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM