显示进度条,直到从服务器 C# 接收到数据 [英] Showing Progress Bar until the data is received from the server C#

查看:137
本文介绍了显示进度条,直到从服务器 C# 接收到数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个小工具,可以获取文件大小和 URL 名称,但是当我运行代码并输入文件 URL 时需要时间(大约 4 秒),用户需要时间,看起来像这样不工作.

我想在收到数据之前显示一个进度条,这样用户就不会认为应用程序没有运行.

使用系统;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Data;使用 System.Drawing;使用 System.Linq;使用 System.Text;使用 System.Windows.Forms;命名空间 Multi_Tool{公共部分类 Form1 :表单{公共 Form1(){初始化组件();}private void button2_Click(object sender, EventArgs e){{if (textBox1.Text == ""){MessageBox.Show("您没有输入网址", "网址错误",MessageBoxButtons.OK, MessageBoxIcon.Error);}别的{字符串 URL = textBox1.Text;字符串文件类型 = URL.Substring(URL.LastIndexOf(".") + 1,(URL.Length - URL.LastIndexOf(".") - 1));FileType.Text = filetype.ToUpper();字符串文件名 = URL.Substring(URL.LastIndexOf("/") + 1,(URL.Length - URL.LastIndexOf("/") - 1));namelabel.Text = 文件名;System.Net.WebRequest req = System.Net.HttpWebRequest.Create(textBox1.Text);req.Method = "HEAD";System.Net.WebResponse resp = req.GetResponse();长内容长度 = 0;长期结果;if (long.TryParse(resp.Headers.Get("Content-Length"), out ContentLength)){字符串文件_大小;如果(内容长度 >= 1073741824){结果 = ContentLength/1073741824;kbmbgb.Text = "GB";}否则如果(内容长度 >= 1048576){结果 = ContentLength/1048576;kbmbgb.Text = "MB";}别的{结果 = 内容长度/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();////进度条1//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;//构造函数公共 frmProgress(字符串文本){this.Text = 文字;初始化组件();}

<块引用>

如果您想在进度条中显示值,请确保将 properties 更改回正常(截至目前,它们在回答中设置为 marquee).但是正如您所说,只需 4 秒,最好使用 marquee

//增加进度条值的方法公共无效 PrgBarInc(){如果 (this.IsHandleCreated){如果(this.InvokeRequired){this.Invoke(new MethodInvoker(PrgBarInc));}别的{prgBar.Increment("你的值");}}

==============================主界面类================================

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);//在你的方法中添加启动下载public void PerformDownload(){ProgressForm = new frmProgress("你的文字");ProgressForm.ShowInTaskbar = false;backgroundWorker1.RunWorkerAsync();ProgressForm.ShowDialog();}私有无效backgroundWorker1_DoWork(对象发送者,DoWorkEventArgs e){//执行服务请求//如果您的任何任务被完成,只需调用//ProgressForm.PrgBarInc()}private void backgroundWorker1_RunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e){ProgressForm.Close();ProgressForm.Dispose();}

I have a made a small tool which gets file size and name of the URL but it takes time (approx 4 seconds) when I run the code and enter a file URL it takes time for the user it will look like it is not working.

I want to show a progress bar until the data is received so that user may not think that the application is not working.

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

解决方案

You can use a background worker to move your downloading to another thread and showing a progress bar until the data is received.

ProgressForm is a form which contains progress bar which you can show until data is downloaded

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

If you want to show value in your progress bar make sure to change the properties back to normal(which are set as marquee in answer as of now).But as you said it is going to take just 4 secs it would be good to use 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");
    }
}

==============================Main UI class==============================

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

这篇关于显示进度条,直到从服务器 C# 接收到数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆