110,499
社区成员
发帖
与我相关
我的任务
分享
//主窗体
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
delegate void dShowForm();
Form7 frm = null;
private void Form6_Load(object sender, EventArgs e)
{
}
//显示窗体
void ShowForm()
{
if (this.InvokeRequired)
{
this.Invoke(new dShowForm(this.ShowForm));
}
else
{
frm.ShowDialog(this);
}
}
//控制进度 ----------进度条显示快慢 在这里控制
void SetProgress()
{
for (int i = 1; i <= 50; i++)
{
if (frm.DialogResult == DialogResult.Cancel)
{
break;
}
else
{
//模拟进度
if (i % 2 == 0)
{
frm.SetProgress(50, i, "请稍候......");
}
else
{
frm.SetProgress(50, i, "请稍候...");
}
System.Threading.Thread.Sleep(50);
}
}
}
private void button1_Click_1(object sender, EventArgs e)
{
frm = new Form7();
new System.Threading.Thread(new System.Threading.ThreadStart(ShowForm)).Start();
new System.Threading.Thread(new System.Threading.ThreadStart(SetProgress)).Start();
}
}
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
}
public delegate void dSetProgress(int total, int current,string info);
public void SetProgress(int total, int current,string info)
{
if (this.InvokeRequired)
{
try
{
this.Invoke(new dSetProgress(this.SetProgress), new object[] { total, current,info });
}
catch { }
}
else
{
this.progressBar1.Maximum = total;
this.progressBar1.Value = current;
this.Text = info;
if (progressBar1.Maximum == progressBar1.Value)
{
this.Close();
}
}
}
//取消
private void button1_Click_1(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
}
/*希望对新手有点用.........................*/
/*帖子掉的好快哟 .......
我不想做一个假的进度条 就像 hecker728 哪里说的 能够实时更新 并且点击取消Button 能够结束当前任务的
不知道各位看懂了没
比如我查询一不数据量非常大的数据
当开始查询的时候 等待窗体ProgressForm出现 进度条在动 我看进度条动的太慢 我点击取消
点击取消后 关闭 等待窗体ProgressForm 同时也要结束查询
大侠们帮忙呢??????????????有例子更好,发的代码 我自己还要一个一个试啊......*/
进度条是一个软件人性化考虑之一,他给用户的感觉就是程序内部在不停的动作,执行到了什么程度,而不是整个界面僵死,以至于用户不知道程序在做什么!
看了好几个WinForm程序了,发现他们对进度条的处理完全失去了进度条的作用。他们都是采用Timer来处理,在线程结束的时候,直接赋值进度条达到100%。和我以前做WebForm程序的时候完全不一样,做WebForm程序的时候,进度条是根据总体数据和每步执行后而计算和更新的。在看了这几个WinForm程序后,我在想:是否所有WinForm程序,在进度条的处理上都不能保证实时进度显示?
其实用Timer来处理,不停的更新进度条只是程序作者偷懒的方法。当然这样的好处就是可以简单化处理进度条,代码量少,不易出错,调试方便。
还有一种方法,就是可以及时更新进度条的数据的。那就是采用事件驱动机制,在子线程中监视复杂处理过程中的设定的事件,及时更新!直接看代码:
程序代码using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1
{
/// <summary>
/// Form1 类
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//用子线程工作
new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
}
//开始下载
public void StartDownload()
{
Downloader downloader = new Downloader();
downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
downloader.Start();
}
//同步更新UI
void downloader_onDownLoadProgress(long total, long current)
{
if (this.InvokeRequired)
{
this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
}
else
{
this.progressBar1.Maximum = (int)total;
this.progressBar1.Value = (int)current;
}
}
}
/// <summary>
/// 下载类(您的复杂处理类)
/// </summary>
public class Downloader
{
//委托
public delegate void dDownloadProgress(long total,long current);
//事件
public event dDownloadProgress onDownLoadProgress;
//开始模拟工作
public void Start()
{
for (int i = 0; i < 100; i++)
{
if (onDownLoadProgress != null)
onDownLoadProgress(100, i);
System.Threading.Thread.Sleep(100);
}
}
}
}=================================ling========================================
delegate object dlExecuteQuery();
private void button1_Click(object sender, EventArgs e)
{
dlExecuteQuery de=new dlExecuteQuery(this.Query());
IAsyncResult ir = de.BeginInvoke(null, null);
Form f=new Form()
f.ShowDialog(this);
Application.DoEvents();
while (!ir.IsCompleted)
{
Application.DoEvents();
}
object obj = de.EndInvoke(ir);
f.Close();
}
private object Query()
{
//长时间的操作
}
this.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
this.backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
MethodA(worker,e);
if (worker.CancellationPending)
{
e.Cancel = true;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressPercent.Value = e.ProgressPercentage;
}
private void MethodA(BackgroundWorker worker, DoWorkEventArgs e)
{
int totalPercent =100;
int perComplete = 0;
if (perComplete != (int)(((float)i / totalPercent) * 100))
{
perComplete = (int)(((float)i / totalPercent) * 100);
worker.ReportProgress(perComplete);
}
}