Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示进度条,直到从服务器C接收到数据#_C#_Winforms_Progress Bar - Fatal编程技术网

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

C# 显示进度条,直到从服务器C接收到数据#,c#,winforms,progress-bar,C#,Winforms,Progress Bar,我有一个小工具,它可以获取文件大小和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.For

我有一个小工具,它可以获取文件大小和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(); 
        }  
    }
}
我想在收到数据之前显示一个进度条,这样用户就不会认为应用程序不工作了

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

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

progressBar1.Value = N;
将其包装在BeginInvoke()中可使UI响应:

Dispatcher.BeginInvoke(DispatcherPriority.Normal,
    new DispatcherOperationCallback(delegate
               {
                   progressBar1.Value = progressBar1.Value + 1;
                   //update in UI Thread
                   return null;
               }), null);
您可以使用后台工作程序将下载移动到另一个线程,并显示进度条,直到收到数据

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();
}
如果您想在进度条中显示值,请确保将
属性
更改回正常状态(从现在起,在回答中设置为
选取框
),但正如您所说,只需4秒钟,最好使用
选取框

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

如果这是我的任务,我会在usercontrol中添加一个marqee进度条:)--真的。。你被困在哪里了?如果你的工具是Web应用程序,那么有很多选项可以显示进度条&如果它是Windows应用程序,用来向用户发出他必须等待一段时间的信号,那么你可以使用,因为一代又一代的人看着这件事,或多或少平静地等待着;)如果我尝试这样做,当用户单击“获取文件大小”进度条(选框)时,将从另一个线程开始,直到label.text发生更改,