http://www.cnblogs.com/kisstome88/archive/2009/05/31/1492642.html
C#进度条(Backgroudworker)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 进度条2
{
public partial class Form1 : Form
{
private BackgroundWorker worker = new BackgroundWorker();
int N = 0;
public Form1()
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork);
}
public void DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ComputeFibonacci(worker, e);
}
public void ProgessChanged(object sender, ProgressChangedEventArgs e)
{
progressBarX1.Value = e.ProgressPercentage;
int V =(int)( e.ProgressPercentage / N);
progressBarX1.Text = Convert.ToString(V) + "%";
}
public void CompleteWork(object sender, RunWorkerCompletedEventArgs e)
{
progressBarX1.Text = "处理完毕!";
}
private int ComputeFibonacci(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 92800; i++)
{
if (worker.CancellationPending)
{
e.Cancel = true;
return -1;
}
else
{
worker.ReportProgress(i); //引发ProgessChanged事件
}
}
return -1;
}
private void btnStart_Click(object sender, EventArgs e)
{
N = 92800 / 100;
progressBarX1.Maximum = 92800;
worker.RunWorkerAsync(); //开始执行后台操作
}
private void btnPause_Click(object sender, EventArgs e)
{
worker.CancelAsync(); //请求暂停后台操作
}
}
}
C#如何判断线程池中所有的线程是否已经完成
http://www.soaspx.com/dotnet/csharp/csharp_20090918_381.html
其实很简单用ThreadPool.RegisterWaitForSingleObject方法注册一个定时检查线程池的方法,在检查线程的方法内调用ThreadPool.GetAvailableThreads与ThreadPool.GetMaxThreads并比较两个方法返回的值是不是相等,相等表示线池内所有的线程已经完成.
//每秒检次一次线程池的状态 RegisteredWaitHandle rhw = ThreadPool.RegisterWaitForSingleObject(AutoResetEvent(false), this.CheckThreadPool, null, 1000, false); //检查线程池的方法 private void CheckThreadPool(object state, bool timeout) { int workerThreads = 0; int maxWordThreads = 0; //int int compleThreads = 0; ThreadPool.GetAvailableThreads(out workerThreads, out compleThreads); ThreadPool.GetMaxThreads(out maxWordThreads, out compleThreads); //当可用的线数与池程池最大的线程相等时表示线程池中所有的线程已经完成 if (workerThreads == maxWordThreads) { //当执行此方法后CheckThreadPool将不再执行 rhw.Unregister(null); //此处加入所有线程完成后的处理代码 } }
C#窗口总是显示在最前面
private void Form1_Load(object sender, EventArgs e) { this.TopMost = true; //窗体总在最前 this.StartPosition = FormStartPosition.CenterScreen; //设置窗体的位置
this.FormBorderStyle = FormBorderStyle.None; // 窗口最大化时不显示任务栏。 this.WindowState = FormWindowState.Maximized; //窗口最大化。
}
//--------------------------------弹出窗口方式打开窗口。
form1 fnew = new form1();
fnew.ShowDialog();
|
还可以判断state,校验所有的线程是否是真
Component.DesignMode 属性用于判断组件是否处于IDE设计状态,但不能在构造函数中判断,因为组件还未建立完成,可以置于组件载入的事件中,如:Form的load事件。
如果要在构造函数中判断,可以用:
if(System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
{
//你的代码
}