与背景工作者的进展酒吧Winforms的 [英] Progress Bar With Background Worker In Winforms

查看:66
本文介绍了与背景工作者的进展酒吧Winforms的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guyzz,

Hello Guyzz,

我想在Windows窗体应用程序中使用进度条。实际上我正在将视频上传到Twitter帐户,我想显示进度条以及上传。

I want to use Progress Bar in my Windows Form application. Actually I am uploading video to twitter account, and i wanted to show progress bar along with that upload.

先谢谢。

推荐答案

您好,

对于您的问题,如果您有关于BackgroundWorker控件的问题。接下来,我将向您展示如何使用Background Worker。一个简单的演示供您参考。

For your question, If you have problem about BackgroundWorker control. Next I will show you how to use Background Worker. A simple demo for your reference.

public partial class Form1 : Form { public Form1() { InitializeComponent(); } BackgroundWorker bgworker; private void Form1_Load(object sender, EventArgs e) { bgworker = new BackgroundWorker(); bgworker.DoWork += backgroundWorker_DoWork; ; bgworker.RunWorkerCompleted += backgroundWorker_Completed; bgworker.ProgressChanged += Bgworker_ProgressChanged; bgworker.WorkerReportsProgress = true; bgworker.WorkerSupportsCancellation = true; } private void Bgworker_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) label1.Text = @"The user cancelled the operation"; else label1.Text = @"operation done"; } private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) {

// TODO:上传视频代码

//TODO : Upload video code


for(int i = 0; i< = 100; i ++)
{
if(bgworker.CancellationPending)
{
e.Cancel =真正;
返回;
}
Thread.Sleep(100);
bgworker.ReportProgress(i);
}
}

private void btn_Start_Click(object sender,EventArgs e)
{
if(!bgworker.IsBusy)
{
bgworker.RunWorkerAsync();
btn_Start.Enabled = false;
}
}

private void Stop_Click(对象发送者,EventArgs e)
{
bgworker.CancelAsync();
}

///////////////////////////////////// //////////////////////////////////////////// $ b $ // // ////////////////////////////////////////////////// /////////////////////////////
///< summary>
/// Designer支持的必需方法 - 不要使用代码编辑器修改
///此方法的内容。
///< / summary>
private void InitializeComponent()
{
this.btn_Start = new System.Windows.Forms.Button();
this.Stop = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btn_Start
//
this.btn_Start.Location = new System.Drawing.Point(12,25);
this.btn_Start.Name =" btn_Start" ;;
this.btn_Start.Size = new System.Drawing.Size(75,23);
this.btn_Start.TabIndex = 0;
this.btn_Start.Text =" Start" ;;
this.btn_Start.UseVisualStyleBackColor = true;
this.btn_Start.Click + = new System.EventHandler(this.btn_Start_Click);
//
//停止
//
this.Stop.Location = new System.Drawing.Point(233,25);
this.Stop.Name =" Stop" ;;
this.Stop.Size = new System.Drawing.Size(75,23);
this.Stop.TabIndex = 0;
this.Stop.Text =" Stop" ;;
this.Stop.UseVisualStyleBackColor = true;
this.Stop.Click + = new System.EventHandler(this.Stop_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(12,83);
this.progressBar1.Name =" progressBar1" ;;
this.progressBar1.Size = new System.Drawing.Size(296,23);
this.progressBar1.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(109,129);
this.label1.Name =" label1" ;;
this.label1.Size = new System.Drawing.Size(0,13);
this.label1.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(353,353);
this.Controls.Add(this.label1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.Stop);
this.Controls.Add(this.btn_Start);
this.Name =" Form1" ;;
this.Text =" Form1" ;;
this.Load + = new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}


private System.Windows.Forms.Button btn_Start;
private System.Windows.Forms.Button Stop;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label label1;
}

for (int i = 0; i <= 100; i++) { if (bgworker.CancellationPending) { e.Cancel = true; return; } Thread.Sleep(100); bgworker.ReportProgress(i); } } private void btn_Start_Click(object sender, EventArgs e) { if (!bgworker.IsBusy) { bgworker.RunWorkerAsync(); btn_Start.Enabled = false; } } private void Stop_Click(object sender, EventArgs e) { bgworker.CancelAsync(); } ///////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////// /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btn_Start = new System.Windows.Forms.Button(); this.Stop = new System.Windows.Forms.Button(); this.progressBar1 = new System.Windows.Forms.ProgressBar(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btn_Start // this.btn_Start.Location = new System.Drawing.Point(12, 25); this.btn_Start.Name = "btn_Start"; this.btn_Start.Size = new System.Drawing.Size(75, 23); this.btn_Start.TabIndex = 0; this.btn_Start.Text = "Start"; this.btn_Start.UseVisualStyleBackColor = true; this.btn_Start.Click += new System.EventHandler(this.btn_Start_Click); // // Stop // this.Stop.Location = new System.Drawing.Point(233, 25); this.Stop.Name = "Stop"; this.Stop.Size = new System.Drawing.Size(75, 23); this.Stop.TabIndex = 0; this.Stop.Text = "Stop"; this.Stop.UseVisualStyleBackColor = true; this.Stop.Click += new System.EventHandler(this.Stop_Click); // // progressBar1 // this.progressBar1.Location = new System.Drawing.Point(12, 83); this.progressBar1.Name = "progressBar1"; this.progressBar1.Size = new System.Drawing.Size(296, 23); this.progressBar1.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(109, 129); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(0, 13); this.label1.TabIndex = 2; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(353, 353); this.Controls.Add(this.label1); this.Controls.Add(this.progressBar1); this.Controls.Add(this.Stop); this.Controls.Add(this.btn_Start); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.Button btn_Start; private System.Windows.Forms.Button Stop; private System.Windows.Forms.ProgressBar progressBar1; private System.Windows.Forms.Label label1; }

希望它对您有所帮助。

最好的问候,

Bob


这篇关于与背景工作者的进展酒吧Winforms的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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