C# 怎样使用进度条
我想在程序进行操作的时候,比如在对数据库进行删除操作的时候,因为数据量比较大,所以往往要等待很长的时间,此时界面无任何反应,所以想在执行删除操作的时候,界面上面显示进度条...
我想在程序进行操作的时候,比如在对数据库进行删除操作的时候,因为数据量比较大,所以往往要等待很长的时间,此时界面无任何反应,所以想在执行删除操作的时候,界面上面显示进度条显示执行到了哪里,然后删除结束成功以后进度条显示100%,请问该怎样进行操作
展开
4个回答
展开全部
这个实现的方法很多,可以用代理,BackgroundWorker, Task,Thread等等,但原理差不多,就是开一个新线程让其执行耗时操作,过程中异步更新UI界面。
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace AskAnswers
{
public class ShowProgressStatus : Form
{
[STAThread]
static void Main()
{
Application.Run(new ShowProgressStatus());
}
public ShowProgressStatus()
{
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.btnProcess = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
//@design this.TrayHeight = 0;
//@design this.TrayLargeIcon = false;
//@design this.TrayAutoArrange = true;
label1.Location = new System.Drawing.Point(32, 40);
label1.Text = "Progress Value";
label1.Size = new System.Drawing.Size(88, 24);
label1.TabIndex = 2;
progressBar1.Maximum = 10;
progressBar1.Location = new System.Drawing.Point(8, 312);
progressBar1.Minimum = 0;
progressBar1.TabIndex = 0;
progressBar1.Value = 0;
//We have calculated the excat size which will result in only 20 boxes to be drawn
progressBar1.Size = new System.Drawing.Size(520, 40);
progressBar1.Step = 1;
btnProcess.Location = new System.Drawing.Point(152, 168);
btnProcess.Size = new System.Drawing.Size(144, 48);
btnProcess.TabIndex = 1;
btnProcess.Text = "Process";
btnProcess.Click += new System.EventHandler(btnProcess_Click);
textBox1.Location = new System.Drawing.Point(136, 40);
textBox1.Text = "0";
textBox1.TabIndex = 3;
textBox1.Size = new System.Drawing.Size(184, 20);
this.Text = "Display Progress Status";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(616, 393);
this.StartPosition = FormStartPosition.CenterScreen;
this.Controls.Add(textBox1);
this.Controls.Add(label1);
this.Controls.Add(btnProcess);
this.Controls.Add(progressBar1);
}
void btnProcess_Click(object sender, EventArgs e)
{
if (isProcessRunning)
{
MessageBox.Show("A process is already running.");
return;
}
Task<string[]>.Factory.StartNew(() => {
isProcessRunning = true;
return Directory.GetFiles(@"C:\Windows\system", "*");
})
.ContinueWith(files => {
string[] filesResult = files.Result;
progressBar1.Maximum = filesResult.Length;
Console.WriteLine("The Maximum of Progress Bar " + progressBar1.Maximum);
return filesResult;
})
.ContinueWith(files => {
string[] filesResult = files.Result;
Console.WriteLine("The files count " + filesResult.Length);
for (int n = 0; n < filesResult.Length; n++ )
{
Thread.Sleep(100);
Console.WriteLine(filesResult[n]);
progressBar1.Value = n + 1;
}
})
.ContinueWith(files => {
MessageBox.Show("Thread completed!");
progressBar1.BeginInvoke(
new Action(() =>
{
progressBar1.Value = 0;
}
));
isProcessRunning = false;
});
// .Wait();
// string[] files = Directory.GetFiles(@"c:\", "*");
// var filesCount = files.Length;
// progressBar1.Maximum = filesCount;
// Thread.Sleep(50);
// Thread backgroundThread = new Thread(
// new ThreadStart(() =>
// {
// isProcessRunning = true;
// for (int n = 0; n < filesCount; n++ )
// {
// Thread.Sleep(100);
// Console.WriteLine(files[n]);
// progressBar1.BeginInvoke(
// new Action(() =>
// {
// progressBar1.Value = n;
// }
// ));
// }
// MessageBox.Show("Thread completed!");
// progressBar1.BeginInvoke(
// new Action(() =>
// {
// progressBar1.Value = 0;
// }
// ));
// isProcessRunning = false;
// }
// ));
// backgroundThread.Start();
}
private System.Windows.Forms.Button btnProcess;
private System.ComponentModel.Container components;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ProgressBar progressBar1;
private bool isProcessRunning;
}
}
展开全部
这不太好整吧...
如果非要做,你还得先查一下需要删除多少条记录,然后还得监控已删除多少条记录,频繁访问数据库不值当的...
不如就做一个ProgressBar,把它的Style属性改为Marquee,做一个“等待”的进度条就可以了。
如果非要做,你还得先查一下需要删除多少条记录,然后还得监控已删除多少条记录,频繁访问数据库不值当的...
不如就做一个ProgressBar,把它的Style属性改为Marquee,做一个“等待”的进度条就可以了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.把进度条控件从工具箱中拖出
2.用线程启动查询方法
如:
progressBar.Value = 0;
progressBar.Visible = true;
new System.Threading.Thread(delegate()
{
while (progressBar.Value + 10 < progressBar.Maximum)
{
this.Invoke(func, progressBar.Value + 10);
System.Threading.Thread.Sleep(100);
}
//下载完结
{
this.Invoke(downloadOverFunc);
}
}).Start();
3.在查询方法中添加进度条事件
4,当查询方法返回值时出发进度条事件
2.用线程启动查询方法
如:
progressBar.Value = 0;
progressBar.Visible = true;
new System.Threading.Thread(delegate()
{
while (progressBar.Value + 10 < progressBar.Maximum)
{
this.Invoke(func, progressBar.Value + 10);
System.Threading.Thread.Sleep(100);
}
//下载完结
{
this.Invoke(downloadOverFunc);
}
}).Start();
3.在查询方法中添加进度条事件
4,当查询方法返回值时出发进度条事件
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用backgroundworkder
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询