[英]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.