C#实现进度条方法很多,一般项目中简单的进度条就可以了,但不管是委托还是多线程发现都有进度条UI显示滞后的问题。一般是让线程Sleep(500),但我发现还是有点不同步,可能是我太菜了,后来找到使用进度条空间的step()方法可以避免进度条进度与提示不同步问题,这里为了能使进度条控件使用方便,写了个工具类调用,这样简单方便通用。测试代码如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e) { try { ProgreeeBarUtil.ShowProgreeBarFrm(this,0,10000,1); for (int i = 0; i <= 10000; i++) { //object[] objs = new object[] { i, string.Format("当前进度{0}", i) }; //ProgreeeBarUtil.SetProgreessBarValue(objs); ProgreeeBarUtil.SetProgreessBarTip(string.Format("当前进度{0}", i)); } ProgreeeBarUtil.ClosePProgreeBarFrm();
} catch (Exception ex) { } }
} public partial class ProgressBarFrm : Form { public ProgressBarFrm() { InitializeComponent(); this.progressBar1.Minimum = 0; this.progressBar1.Maximum = 100; }
public ProgressBarFrm(int Minimum, int Maximum,int step) { InitializeComponent(); this.progressBar1.Minimum = Minimum; this.progressBar1.Maximum = Maximum; this.progressBar1.Step = step; } }
/// /// 进度条工具类 /// public class ProgreeeBarUtil { private static Thread thread;
private static Form parentForm; private static ProgressBarFrm progressBarFrm;
private delegate void SetValue(object value);
#region 设置进度条风格
#endregion
public static void ShowProgreeBarFrm(object parentFrm) { parentForm = parentFrm as Form; progressBarFrm = new ProgressBarFrm(); progressBarFrm.StartPosition = FormStartPosition.CenterParent; if (parentForm != null) { progressBarFrm.Show(parentForm); parentForm.Enabled = false; StartThread(); }
}
public static void ClosePProgreeBarFrm() { progressBarFrm.Close(); parentForm.Enabled = true; }
public static void ShowProgreeBarFrm(object parentFrm,int minNum,int maxNum,int step) { parentForm = parentFrm as Form; progressBarFrm = new ProgressBarFrm(minNum, maxNum,step); progressBarFrm.StartPosition = FormStartPosition.CenterParent; if (parentForm != null) { progressBarFrm.Show(parentForm); parentForm.Enabled = false; StartThread(); } }
public static void InitProgreeBarParam(object[] objs) { }
public static void SetMinValue(int minNumber) { progressBarFrm.progressBar1.Minimum = minNumber; } public static void SetMaxValue(int maxNumber) { progressBarFrm.progressBar1.Minimum = maxNumber; } public static void SetStep(int step) { progressBarFrm.progressBar1.Step = step; }
/// /// 重置进度条 /// /// /// /// public static void ResetProgressBar(int maxNumber, int step, int value) { progressBarFrm.progressBar1.Minimum = maxNumber; progressBarFrm.progressBar1.Step = step; progressBarFrm.progressBar1.Value = value; Application.DoEvents(); }
public static void StartThread() { thread = new Thread(SetProgreessBarValue); thread.IsBackground = true; thread.Start(); }
/// /// 传当前进度值 /// /// public static void SetProgreessBarValue(object value) { if (progressBarFrm.InvokeRequired) { SetValue setvalue = SetProgreessBarValue; progressBarFrm.Invoke(setvalue, value); } else { progressBarFrm.progressBar1.PerformStep(); progressBarFrm.label1.Text = Convert.ToInt32(value).ToString(); Application.DoEvents(); Thread.Sleep(100);
}
}
public static void SetProgreessBarValues(object values) { if (progressBarFrm.InvokeRequired) { SetValue setvalue = SetProgreessBarValue; progressBarFrm.Invoke(setvalue, values); } else { if (values != null) { object[] objs = values as object[]; int value = Convert.ToInt32(objs[0]); string tipStr = objs[1].ToString(); progressBarFrm.progressBar1.Value = value; progressBarFrm.label1.Text = tipStr; Thread.Sleep(100); Application.DoEvents();
} } }
/// /// 传提示信息 /// /// public static void SetProgreessBarTip(object value) { if (progressBarFrm.InvokeRequired) { SetValue setvalue = SetProgreessBarValue; progressBarFrm.Invoke(setvalue, value); } else { if (value != null) { string tipStr = value.ToString(); progressBarFrm.progressBar1.PerformStep(); progressBarFrm.label1.Text = tipStr; Application.DoEvents();
} } } } |