看了好几个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 void btnCount_Click(object sender, EventArgs e)
{
label1.Visible=true;
progressBar.Visible = true;
progressBar.Minimum = 0;
progressBar.Maximum = ds.Tables["表"].Rows.Count;
progressBar.BackColor = Color.Green;
for (int i = 0; i < ds.Tables["表"].Rows.Count; i++)
{
progressBar.Value++;
Application.DoEvents();
this.label1.Text = Convert.ToString(progressBar.Value);this.label1.Refresh();
}
}
http://blog.163.com/light_warm/blog/static/3168104200861710226745/
另:
建立一个listBox将进程名称遍历进去 this.listBox1.Items.Clear();
Process[] MyProcesses=Process.GetProcesses();
foreach(Process MyProcess in MyProcesses)
{
this.listBox1.Items.Add(MyProcess.ProcessName);
}
this.listBox1.SelectedIndex=0;
选中listBox里面的项后将进程详细信息显示在右面的Label中 try
{
string ProcessName=this.listBox1.Text;
this.groupBox1.Text=ProcessName+"进程的详细信息";
Process[] MyProcess=Process.GetProcessesByName(ProcessName);
this.label1.Text="进程影象名:"+MyProcess[0].ProcessName;
this.label2.Text="进程ID:"+MyProcess[0].Id;
this.label3.Text="启动线程树:"+MyProcess[0].Threads.Count.ToString();
this.label4.Text="CPU占用时间:"+MyProcess[0].TotalProcessorTime.ToString();
this.label5.Text="线程优先级:"+MyProcess[0].PriorityClass.ToString();
this.label6.Text="启动时间:"+MyProcess[0].StartTime.ToLongTimeString();
this.label7.Text="专用内存:"+(MyProcess[0].PrivateMemorySize/1024).ToString()+"K";
this.label8.Text="峰值虚拟内存:"+(MyProcess[0].PeakVirtualMemorySize/1024).ToString()+"K";
this.label9.Text="峰值分页内存:"+(MyProcess[0].PeakPagedMemorySize/1024).ToString()+"K";
this.label10.Text="分页系统内存:"+(MyProcess[0].PagedSystemMemorySize/1024).ToString()+"K";
this.label11.Text="分页内存:"+(MyProcess[0].PagedMemorySize/1024).ToString()+"K";
this.label12.Text="未分页系统内存:"+(MyProcess[0].NonpagedSystemMemorySize/1024).ToString()+"K";
this.label13.Text="物理内存:"+(MyProcess[0].WorkingSet/1024).ToString()+"K";
this.label14.Text="虚拟内存:"+(MyProcess[0].VirtualMemorySize/1024).ToString()+"K";
}
catch(Exception Err)
{
MessageBox.Show("没有此进程,无法获取信息!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//不处理异常
}
联系客服