进度条问题,回答者另加40分,在另一个贴上,晚上没人看到,所以重发.

footprint2008 2009-04-24 08:14:59
有一个窗体A,上面有一个按钮 mybutton1 点击后会执行一段要运行要很长时间的函数“fun()”。
我想给它加上进度条。
思路是这样的。在fun()函数前打开B窗体上面放一个进度条。(专门用于放进度条,没有边框的)。等执行完fun()后,关闭B窗体。
步骤:
新建了一个窗体B。上面放了一个进度条控件"mypro"。
在load_B事件中写下如代码(代码大小写有错,这个忽略,我主要是写出我的思路)

form B 的代码:
load_B(object sender, EventArgs e)

B.maximun=10000000;
while(1==1)
{
for(i <0;i <10000000;i++)

if(i%100==0)
{
mypro.value=i;
}

}

------------------------------
form A

button1_click(object sender, EventArgs e)
{

B b=new B();
b.show();//打开进度条窗体,问题出现了,这时进度条走的效果很差,跟本看不到渐增的效果。
fun()
{
………………;
}

}


---------------------------------
要求:给出代码,希望窗体B的进度条走的很清晰。(一格一格的走,不要看都看不见就满了,或fun()执行完了进度条才走。要边一边执行fun(),进度条一边一格一格地走。)

另外,我也试了用多线程异步调用(可能我的水平太差,不正确吧)。总之就是看不到窗体B进度条“mypro”一格一格走的样子。搞了一天,我要疯了。
...全文
253 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
footprint2008 2009-04-26
  • 打赏
  • 举报
回复
谢谢大家,还有很多人值得感谢,可惜分太少。。。。。。。
footprint2008 2009-04-25
  • 打赏
  • 举报
回复
辛苦了!这么长的代码.
Sysping1 2009-04-25
  • 打赏
  • 举报
回复
1: 换了个帐号才终于贴完代码
2: 把以上代码组起来,就是一个完整通用的进度窗体工具类
3:调用简单,只有把想加进度的代码在外部如下套起来就可以了
frm_Public_Process.ExcuteByProgress
(
delegate(){
..你的代码...
}
);
AssetSystem 2009-04-25
  • 打赏
  • 举报
回复
继续...
//frm_Public_Process.designer.cs(画面代码)
namespace Sysping.Presentation{
partial class frm_Public_Process{
private System.ComponentModel.IContainer components = null;
public System.ComponentModel.ComponentResourceManager resources = null;
protected override void Dispose(bool disposing){
if (disposing && (components != null)){
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent(){
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm_Public_Process));
this.pan_Flash = new System.Windows.Forms.Panel();
this.pcb_This = new System.Windows.Forms.PictureBox();
this.lab_Tip = new System.Windows.Forms.Label();
this.lab_Span = new System.Windows.Forms.Label();
this.btn_Close = new DevExpress.XtraEditors.SimpleButton();
this.prg_This = new System.Windows.Forms.ProgressBar();
this.tim_This = new System.Windows.Forms.Timer(this.components);
this.pan_Flash.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pcb_This)).BeginInit();
this.SuspendLayout();
this.pan_Flash.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pan_Flash.Controls.Add(this.pcb_This);
this.pan_Flash.Controls.Add(this.lab_Tip);
this.pan_Flash.Controls.Add(this.lab_Span);
this.pan_Flash.Controls.Add(this.btn_Close);
this.pan_Flash.Controls.Add(this.prg_This);
this.pan_Flash.Dock = System.Windows.Forms.DockStyle.Fill;
this.pan_Flash.Location = new System.Drawing.Point(0, 0);
this.pan_Flash.Name = "pan_Flash";
this.pan_Flash.Size = new System.Drawing.Size(222, 79);
this.pan_Flash.TabIndex = 8;
this.pcb_This.Image = ((System.Drawing.Image)(resources.GetObject("pcb_This.Image")));
this.pcb_This.Location = new System.Drawing.Point(40, 25);
this.pcb_This.Name = "pcb_This";
this.pcb_This.Size = new System.Drawing.Size(149, 10);
this.pcb_This.TabIndex = 13;
this.pcb_This.TabStop = false;
this.pcb_This.Visible = false;
this.lab_Tip.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lab_Tip.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.lab_Tip.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lab_Tip.ForeColor = System.Drawing.Color.Maroon;
this.lab_Tip.Location = new System.Drawing.Point(0, 38);
this.lab_Tip.Name = "lab_Tip";
this.lab_Tip.Size = new System.Drawing.Size(220, 26);
this.lab_Tip.TabIndex = 9;
this.lab_Tip.Tag = "";
this.lab_Tip.Text = " Busying...";
this.lab_Tip.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lab_Span.CausesValidation = false;
this.lab_Span.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lab_Span.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lab_Span.ForeColor = System.Drawing.Color.Blue;
this.lab_Span.ImageAlign = System.Drawing.ContentAlignment.BottomCenter;
this.lab_Span.Location = new System.Drawing.Point(0, 64);
this.lab_Span.Name = "lab_Span";
this.lab_Span.Size = new System.Drawing.Size(220, 13);
this.lab_Span.TabIndex = 12;
this.lab_Span.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lab_Span.UseWaitCursor = true;
this.btn_Close.Appearance.BackColor = System.Drawing.Color.Silver;
this.btn_Close.Appearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
this.btn_Close.Appearance.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btn_Close.Appearance.Options.UseBackColor = true;
this.btn_Close.Appearance.Options.UseBorderColor = true;
this.btn_Close.Appearance.Options.UseFont = true;
this.btn_Close.ButtonStyle = DevExpress.XtraEditors.Controls.BorderStyles.HotFlat;
this.btn_Close.Location = new System.Drawing.Point(204, 0);
this.btn_Close.Name = "btn_Close";
this.btn_Close.Size = new System.Drawing.Size(16, 16);
this.btn_Close.TabIndex = 10;
this.btn_Close.Text = "×";
this.btn_Close.Visible = false;
this.btn_Close.Click += new System.EventHandler(this.btn_Close_Click);
this.prg_This.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.prg_This.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.prg_This.ForeColor = System.Drawing.Color.Lime;
this.prg_This.Location = new System.Drawing.Point(39, 25);
this.prg_This.Name = "prg_This";
this.prg_This.Size = new System.Drawing.Size(150, 10);
this.prg_This.Step = 20;
this.prg_This.Style = System.Windows.Forms.ProgressBarStyle.Marquee;
this.prg_This.TabIndex = 0;
this.prg_This.Value = 20;
this.tim_This.Tick += new System.EventHandler(this.tim_This_Tick);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.SkyBlue;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(222, 79);
this.Controls.Add(this.pan_Flash);
this.DoubleBuffered = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "frm_Public_Process";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Processing...";
this.TopMost = true;
this.Load += new System.EventHandler(this.frm_Public_Process_Load);
this.Shown += new System.EventHandler(this.frm_Public_Process_Shown);
this.pan_Flash.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pcb_This)).EndInit();
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.ProgressBar prg_This;
public System.Windows.Forms.Panel pan_Flash;
public System.Windows.Forms.Label lab_Tip;
private DevExpress.XtraEditors.SimpleButton btn_Close;
public System.Windows.Forms.Label lab_Span;
public System.Windows.Forms.Timer tim_This;
private System.Windows.Forms.PictureBox pcb_This;
}
}
AssetSystem 2009-04-25
  • 打赏
  • 举报
回复
继续...
#region 事件
private void frm_Public_Process_Load(object sender, EventArgs e){
this.lab_Tip.Text = frm_Public_Process.JustTip;
this.SetProgressBarStyle();
if ((!frm_Public_Process.IsBusyingIn) && (frm_Public_Process.JustEnableTimeSpan)) this.tim_This.Start();
this.Cursor = this.btn_Close.Visible ? Cursors.Default : Cursors.WaitCursor;
this.lab_Tip.Update();
}
private void frm_Public_Process_Shown(object sender, EventArgs e){
this.Update();
if (frm_Public_Process.JustCallbackMethod != null){
try{
if (frm_Public_Process.IsBusyingIn){
IAsyncResult result = frm_Public_Process.JustCallbackMethod.BeginInvoke(null, null);
while (!result.IsCompleted){
this.lab_Tip.Text = frm_Public_Process.JustTip;
this.lab_Tip.Update();
if (frm_Public_Process.JustEnableTimeSpan) this.tim_This_Tick(null, null);
if (frm_Public_Process.IsFlashMode) this.pcb_This.Update();
else this.prg_This.Update();
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.JustCallbackMethod.EndInvoke(result);
}
else{
this.Invoke(frm_Public_Process.JustCallbackMethod);
if (this.DialogResult == DialogResult.None){
this.DialogResult = DialogResult.OK;
}
}
}
catch (Exception exception){
frm_Public_Process.JustException = exception;
}
finally{
this.Release();
}
}
}
private void SetProgressBarStyle(){
try{
this.pcb_This.Visible = frm_Public_Process.IsFlashMode;
this.prg_This.Visible = !this.pcb_This.Visible;
this.prg_This.Maximum = 100;
this.prg_This.Minimum = 1;
this.prg_This.Value = 10;
this.prg_This.Step = 1;
if (frm_Public_Process.JustProgressModel == ProgressModel.Progressing){
this.prg_This.Style = ProgressBarStyle.Blocks;
}
else{
this.prg_This.Style = ProgressBarStyle.Marquee;
}
}
catch
{ }
}
private void tim_This_Tick(object sender, EventArgs e){
TimeSpan span = DateTime.Now - this.JustStartTime;
this.lab_Span.Text = string.Format("{0:f}", span.TotalSeconds);
this.lab_Span.Visible = frm_Public_Process.JustEnableTimeSpan;
this.lab_Span.Update();
}
private void btn_Close_Click(object sender, EventArgs e){
this.Release();
}
private void Release(){
try{
this.tim_This.Stop();
this.tim_This.Dispose();
this.prg_This.Style = ProgressBarStyle.Blocks;
this.prg_This.Value = this.prg_This.Maximum;
this.Update();
Thread.Sleep(50);
}
finally{
if (this.DialogResult == DialogResult.None){
this.DialogResult = DialogResult.Cancel;
}
this.Cursor = Cursors.Default;
this.Close();
}
}
#endregion
}
}
...待续
AssetSystem 2009-04-25
  • 打赏
  • 举报
回复
继续...
#region 初始化
public frm_Public_Process(){
this.InitializeComponent();
frm_Public_Process.JustException = null;
}
private static frm_Public_Process CreateSingleInstance(){//创建单实例对象
if (frm_Public_Process.JustFrmProcess == null){
frm_Public_Process.JustFrmProcess = new frm_Public_Process();
}
return frm_Public_Process.JustFrmProcess;
}
#endregion
#region 方法.应用
public static void ExcuteByProgress(ProgressModel model, CallbackMethod callbackMethod, bool enableTimeSpan){
frm_Public_Process frmProcess = frm_Public_Process.CreateSingleInstance();
frm_Public_Process.JustEnableTimeSpan = enableTimeSpan;
frm_Public_Process.JustCallbackMethod = callbackMethod;
frm_Public_Process.JustProgressModel = model;
if (callbackMethod != null){
try{
frmProcess.ShowDialog();
frmProcess.Dispose();
frmProcess = null;
}
finally{
frm_Public_Process.JustFrmProcess = null;
}
if ((frm_Public_Process.IsBusyingIn) && (frm_Public_Process.JustException != null)){
throw frm_Public_Process.JustException;
}
}
else{
frmProcess.Show();
}
}
public static void ExcuteByProgress(ProgressModel model, CallbackMethod callbackMethod){
frm_Public_Process.ExcuteByProgress(model, callbackMethod, true);
}
public static void ExcuteByProgress(CallbackMethod callbackMethod){
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut1, callbackMethod);
}
public static void ExcuteByProgress(ProgressModel model){
frm_Public_Process.ExcuteByProgress(model, null);
}
public static void SetProgress(int current, int total){//更新进度条
try{
if (frm_Public_Process.JustFrmProcess != null){
if ((current <= 0) || (total <= 0) || (total < current)) return;
frm_Public_Process.JustFrmProcess.Update();
if (frm_Public_Process.IsFlashMode){
frm_Public_Process.JustFrmProcess.pcb_This.Update();
}
else{
frm_Public_Process.JustFrmProcess.prg_This.Minimum = 1;
frm_Public_Process.JustFrmProcess.prg_This.Maximum = total;
frm_Public_Process.JustFrmProcess.prg_This.Value = current;
frm_Public_Process.JustFrmProcess.prg_This.Update();
}
if (frm_Public_Process.JustFrmProcess.btn_Close.Visible) Application.DoEvents();
}
}
catch { }
}
public static void SetTip(string tip){//更新进度条提示信息
try{
frm_Public_Process.JustTip = tip;
if (!frm_Public_Process.IsBusyingIn){
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustFrmProcess.lab_Tip.Text = tip;
frm_Public_Process.JustFrmProcess.Update();
if (frm_Public_Process.JustFrmProcess.btn_Close.Visible) Application.DoEvents();
}
}
}
catch { }
}
public static void SetTimeSpanEnable(bool timeSpanEnable){//设置是否开启计时
try{
frm_Public_Process.JustEnableTimeSpan = timeSpanEnable;
if (!frm_Public_Process.IsBusyingIn){
if (frm_Public_Process.JustFrmProcess != null){
if (timeSpanEnable){
frm_Public_Process.JustFrmProcess.tim_This.Start();
}
else{
frm_Public_Process.JustFrmProcess.tim_This.Stop();
}
}
}
}
catch { }
}
public static void SetProgressModel(ProgressModel model){//设置进度条模式
try{
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustProgressModel = model;
frm_Public_Process.JustFrmProcess.SetProgressBarStyle();
frm_Public_Process.JustFrmProcess.Update();
}
}
catch { }
}
public static void ShowCancel(bool show){//是否显示取消
try{
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustFrmProcess.Cursor = show ? Cursors.Default : Cursors.WaitCursor;
frm_Public_Process.JustFrmProcess.btn_Close.Visible = show;
frm_Public_Process.JustFrmProcess.Update();
}
}
catch { }
}
public static bool HasClickedCancel(){//是否用户点了取消控制
try{
if (frm_Public_Process.JustFrmProcess != null){
return frm_Public_Process.JustFrmProcess.DialogResult == DialogResult.Cancel;
}
}
catch { }
return false;
}
public static void CancelProgress(){//中途取消进度条
try{
if (frm_Public_Process.JustFrmProcess != null){
frm_Public_Process.JustFrmProcess.Release();
frm_Public_Process.JustFrmProcess.Dispose();
}
}
finally {
frm_Public_Process.JustFrmProcess = null;
}
}
#endregion
...待续
Sysping1 2009-04-24
  • 打赏
  • 举报
回复
继续...
4:代码文件 frm_Public_Process.cs
using System;
using System.Text;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

public delegate void CallbackMethod();//代理方
public delegate void CallbackMethodByParams(params object[] args);//代理方法[带可变参数]

namespace Sysping.Presentation{
public enum ProgressModel{//进度条模式
BusyingOut1 = 0,//仅显示忙状态[忙部分的执行及刷新在外部|以进度条控件显示进度]
BusyingOut2 = 1,//仅显示忙状态[忙部分的执行及刷新在外部|以Flash动画显示进度]
BusyingIn1 = 2,//仅显示忙状态[忙部分的执行在内部|以进度条控件显示忙状态]
BusyingIn2 = 3,//仅显示忙状态[忙部分的执行在内部且以Flash动画显示忙状态]
Progressing = 4//有实际进度[调用方刷新]
}

public partial class frm_Public_Process : Form {//进度窗体
#region 变量
private static bool JustEnableTimeSpan = true;//是否要计时
private DateTime JustStartTime = DateTime.Now;//时间
public static Exception JustException = null;//异常信息
private static string JustTip = "Busying";// 进度提示
private static CallbackMethod JustCallbackMethod = null;//来自调用方的代理方法
private static frm_Public_Process JustFrmProcess = null;//当前进度窗体
private static ProgressModel JustProgressModel = ProgressModel.BusyingOut1;//应用模式标志[忙状态|有进度]
private static bool IsBusyingIn //是忙状态还是无实际进度的模式{
get{return (frm_Public_Process.JustProgressModel == ProgressModel.BusyingIn1) ||
(frm_Public_Process.JustProgressModel == ProgressModel.BusyingIn2);
}
}
private static bool IsFlashMode{//是Flash动画方式的忙状态还是进度条方式的忙状态模式
get{
return (frm_Public_Process.JustProgressModel == ProgressModel.BusyingIn2) ||
(frm_Public_Process.JustProgressModel == ProgressModel.BusyingOut2);
}
}
#endregion
...待续
Sysping1 2009-04-24
  • 打赏
  • 举报
回复
继续...
//有实际进度&非代理调用&有取消[此时完全表现为非模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.Progressing);
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing4");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 50);
Thread.Sleep(100);
}
frm_Public_Process.CancelProgress();

//代理调用
try{
//有实际进度&代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.Progressing,
delegate(){
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing5");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 100);
Thread.Sleep(100);
}
}
);

//有实际进度&代理调用&有取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.Progressing,
delegate(){
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing6");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 100);
if (i == 80){
frm_Public_Process.CancelProgress();
break;
}
if (frm_Public_Process.HasClickedCancel()) break;
Thread.Sleep(100);
}
}
);

//无实际进度&代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut1,
delegate(){
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing7");
for (int i = 1; i <= 20; i++)
{
Thread.Sleep(100);
Application.DoEvents();
}
}
);

//Ex
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut2,
delegate(){
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing7.Flash");
for (int i = 1; i <= 20; i++){
Thread.Sleep(100);
Application.DoEvents();
}
}
);

//无实际进度&代理调用&有取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut1,
delegate(){
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing8");
for (int i = 1; i <= 20; i++)
{
if (frm_Public_Process.HasClickedCancel()) break;
Thread.Sleep(100);
Application.DoEvents();
}
}
);

//Ex
frm_Public_Process.ExcuteByProgress
(
ProgressModel.BusyingOut2,
delegate(){
frm_Public_Process.ShowCancel(true);
frm_Public_Process.SetTip("Testing8.Flash");
for (int i = 1; i <= 20; i++){
if (frm_Public_Process.HasClickedCancel()) break;
Thread.Sleep(100);
Application.DoEvents();
}
}
);
}
finally{
this.btn_TestProgress.Enabled = true;
}
}
...待续
Sysping1 2009-04-24
  • 打赏
  • 举报
回复
贴一个用在技术框架上的一个进度条工具类。
1:以下代码是完整的,虽然有点长,是很全面进度条工具类。
2: 特点是调用简单,几分进度条各种应用情况都考虑到了,肯定可以找到你要用的情况。
3: 下面是一般调用的方,很简单,有多种进度条情况,根据你的需要调用。
private void btn_TestProgress_Click(object sender, EventArgs e){
this.btn_TestProgress.Enabled = false;

//内部忙状态|Flash
try{
frm_Public_Process.ExcuteByProgress(
ProgressModel.BusyingIn2,
delegate(){
SqlConnection con1 = new SqlConnection(
"Data Source=(10.0.0.113);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Connection Timeout=5;"
);
frm_Public_Process.SetTip("Testing.New");
con1.Open();
}
);
}
catch (Exception exception){
MessageBox.Show("excepton1=" + exception.Message, "Message1");
}

//内部忙状态|进度条
try{
frm_Public_Process.ExcuteByProgress(
ProgressModel.BusyingIn1,
delegate(){
SqlConnection con1 = new SqlConnection(
"Data Source=(10.0.0.114);Initial Catalog=AdventureWorks;Integrated Security=SSPI;Connection Timeout=5;"
);
frm_Public_Process.SetTip("Testing.Progress");
con1.Open();
}
);
}
catch (Exception exception)
{
MessageBox.Show("excepton1=" + exception.Message, "Message2");
}

//无实际进度&非代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut1);
frm_Public_Process.ShowCancel(false);
IAsyncResult result1 = this.BeginInvoke(
new CallbackMethod(
delegate(){
frm_Public_Process.SetTip("Testing1");
for (int i = 1; i <= 20; i++)
{
frm_Public_Process.SetProgress(i, 20);
Thread.Sleep(100);
}
}
)
);
while (!result1.IsCompleted){
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.CancelProgress();

//Ex
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut2);
frm_Public_Process.ShowCancel(false);
IAsyncResult result1Ex = this.BeginInvoke(
new CallbackMethod(
delegate(){
frm_Public_Process.SetTip("Testing1.Flash");
for (int i = 1; i <= 20; i++)
{
frm_Public_Process.SetProgress(i, 20);
Thread.Sleep(100);
}
}
)
);
while (!result1Ex.IsCompleted){
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.CancelProgress();

//无实际进度&非代理调用&有取消[此时完全表现为非模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.BusyingOut1);
frm_Public_Process.ShowCancel(true);
IAsyncResult result2 = this.BeginInvoke(
new CallbackMethod(
delegate(){
frm_Public_Process.SetTip("Testing2");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 30);
Thread.Sleep(100);
}
}
)
);
while (!result2.IsCompleted){
Thread.Sleep(100);
Application.DoEvents();
}
frm_Public_Process.CancelProgress();

//有实际进度&非代理调用&无取消[此时完全表现为模式窗体]
frm_Public_Process.ExcuteByProgress(ProgressModel.Progressing);
frm_Public_Process.ShowCancel(false);
frm_Public_Process.SetTip("Testing3");
for (int i = 1; i <= 20; i++){
frm_Public_Process.SetProgress(i, 40);
Thread.Sleep(100);
}
frm_Public_Process.CancelProgress();

...待续
wzuomin 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 xiaoyasheng 的回复:]
你的fun和进度条之间有啥关系没有,如果没有就造个假的,有点话,就循环
[/Quote]

不知道这样行不?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

frmProgressbar frm=null;

[BrowsableAttribute(false)]
public static bool CheckForIllegalCrossThreadCalls { get; set; }


public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls=false ;
}

private void Form1_Load(object sender, EventArgs e)
{
Button btn = new Button { Text = "Test" };
btn.Click += fun;
this.Controls.Add(btn);
}

private void fun(object sender, EventArgs e)
{
frm = new frmProgressbar { Visible = true };
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(fun));
th.Start();
}

private void fun()
{
System.Threading.Thread.Sleep(10000);
if (frm!=null) frm.Close();
}

}

public class frmProgressbar : System.Windows.Forms.Form
{

private ProgressBar probar;
private Timer tmr;

public frmProgressbar()
{
probar = new ProgressBar();
this.Controls.Add(probar);
tmr = new Timer { Interval = 200 };
tmr.Tick += tick;
tmr.Start();
}

private void tick(object sender, EventArgs e)
{
if (probar.Value < probar.Maximum)
{
probar.Value += 1;
}
else
{
probar.Value = probar.Minimum;
}
this.Text = probar.Value.ToString();
}

}

}



xutao888 2009-04-24
  • 打赏
  • 举报
回复
第一,你需要另开一个线程打开B窗口,
第二,B窗口进度条需要使用Sleep()将资源空一些出来用于处理逻辑
第三,B窗口需要使用Application,DoEvents(),否则看不到效果
footprint2008 2009-04-24
  • 打赏
  • 举报
回复
就是一个假的呀,但假的显示的不完整;
xiaoyasheng 2009-04-24
  • 打赏
  • 举报
回复
你的fun和进度条之间有啥关系没有,如果没有就造个假的,有点话,就循环
footprint2008 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 wzuomin 的回复:]
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private…
[/Quote]




如果
private void fun(object sender, EventArgs e)
{
using (frmProgressbar frm = new frmProgressbar { Visible = true })
{
for (int i = 0; i <= 100; i++)
{
this.Text = string.Format("{0}ms", i * 100);
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
}
}



主要是这句代码起了作用,Application.DoEvents();
但如果fun()不是一个循环环体呢?
那该如何做?

wzuomin 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 zhzuo 的回复:]
这个问题很经典,楼主可以完全使用BackgroundWorker来实现,
BackgroundWorker类包含3个事件,在事件处理程序中可进行异步操作辅助代码编写和同用户界面信息交互。

C# codepubliceventDoWorkEventHandler DoWork;//异步处理代码
publiceventProgressChangedEventHandler ProgressChanged;//处理进度汇报,同UI交互
publiceventRunWorkerCompletedEventHandler RunWorkerCompleted;//结束处理通知




代码太多不在…
[/Quote]


这个是完美解决方案
msw731 2009-04-24
  • 打赏
  • 举报
回复
我也有和楼主类似的问题,我在考虑不用弹出另一个窗口的问题,而是把进度条坐在初始窗口上(先隐藏),当在执行你的fun() { ………………; } 之前加上进度条进度条显示并且开始运行,当fun函数运行完后再隐藏并停止进度条.当然在设计时应该考虑多线程运行,以免出现假死.
marvelstack 2009-04-24
  • 打赏
  • 举报
回复
这个问题很经典,楼主可以完全使用BackgroundWorker来实现,
BackgroundWorker类包含3个事件,在事件处理程序中可进行异步操作辅助代码编写和同用户界面信息交互。
publiceventDoWorkEventHandler DoWork;//异步处理代码
publiceventProgressChangedEventHandler ProgressChanged;//处理进度汇报,同UI交互
publiceventRunWorkerCompletedEventHandler RunWorkerCompleted;//结束处理通知


代码太多不在贴出来,这里包含了完整的代码和分析,楼主可以参考后应该不会再有疑问,
http://blog.csdn.net/zhzuo/archive/2008/07/23/2699305.aspx

wzuomin 2009-04-24
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Button btn = new Button { Text = "Test" };
btn.Click += fun;
this.Controls.Add(btn);
}

private void fun(object sender, EventArgs e)
{
using (frmProgressbar frm = new frmProgressbar { Visible = true })
{
for (int i = 0; i <= 100; i++)
{
this.Text = string.Format("{0}ms", i * 100);
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
}
}

}

public class frmProgressbar : System.Windows.Forms.Form
{

private ProgressBar probar;
private Timer tmr;

public frmProgressbar ()
{
probar = new ProgressBar();
this.Controls.Add(probar);
tmr = new Timer { Interval = 200 };
tmr.Tick += tick;
tmr.Start();
}

private void tick(object sender, EventArgs e)
{
if (probar.Value < probar.Maximum) {
probar.Value += 1;
}
else {
probar.Value = probar.Minimum;
}
this.Text = probar.Value.ToString();
}

}

}


qqiuzaihui 2009-04-24
  • 打赏
  • 举报
回复
还是使用线程吧:

private delegate void DoReportProgress(int current);
private event DoReportProgress onReportProgress;

public void DisposeRecord()
{

for (int i=0; i<1000; i++)
{
// 工作

onReportProgress( (100 * i) / 1000 ); //通知进程条更改
}
}

//同步更新进程条
private void OnReportProgress(int current)
{
if (this.InvokeRequired)
{
this.Invoke(new DoReportProgress(OnReportProgress), new object[] { current });
}
else
{
this.tsprogressMain.Maximum = 100;
this.tsprogressMain.Value = (int)current;
}
}


//在子窗体的Load事件中加载线程
private void MainForm_Load(object sender, EventArgs e)
{
this.onReportProgress = new DoReportProgress(OnReportProgress);
  Thread tdDistill = new Thread(new ThreadStart(DisposeRecord)); //使用tdDistill线程用于后台数据处理
  tdDistill.Start();
}

加载更多回复(15)
内容概要:本文围绕双三相永磁同步电机(PMSM)的转速-电流双闭环矢量控制系统,基于Matlab/Simulink平台开展仿真研究,详细构建了电机的数学模型,并设计了外环转速PI控制器与内环电流PI控制器,实现了对电机转速和电流的精确解耦控制。研究涵盖了坐标变换(Clark变换与Park变换)、空间矢量脉宽调制(SVPWM)技术、PI参数整定及系统稳定性析等核心内容,通过仿真验证了该双闭环控制策略在动态响应、抗干扰能力及稳态精度方面的优良性能,为高性能电机驱动系统的设计与优化提供了有效的理论依据和技术参考。; 适合人群:从事电机控制、电力电子与电力传动领域的科研人员、研究生及具备一定Matlab/Simulink基础的工程技术人员。; 使用场景及目标:①深入理解永磁同步电机矢量控制的基本原理与实现方法;②掌握双闭环控制系统的建模、仿真与调试技巧;③学习SVPWM技术及PI控制器在电机控制中的应用与参数整定; 阅读建议:建议读者结合Matlab/Simulink软件,逐步实践文档中提到的建模与仿真步骤,重点关注坐标变换模块、PI控制器参数的调节以及SVPWM发生器的工作原理。通过修改仿真参数,观察系统在启动、加载和调速等不同工况下的动态响应,以加深对控制理论的理解。
内容概要:本文详细介绍了如何利用ESP32结合红外模块实现万能遥控器与智能家电控制系统,涵盖硬件选型、电路连接、红外信号的学习与解码、发射控制、数据持久化存储(NVS)、Wi-Fi联网控制(HTTP/MQTT)以及接入Home Assistant等智能家居平台的完整流程。通过Arduino框架和IRremote库,实现了红外信号捕获、存储、回放及远程控制功能,并提供了学习未知协议的原始时序数据处理方案,支持OTA升级和多设备联动控制。; 适合人群:具备基本电子电路知识和Arduino编程经验的开发者,物联网爱好者,智能家居DIY用户;适合有一定嵌入式开发基础的工作1-3年技术人员进阶实践。; 使用场景及目标:①将传统家电改造为可通过手机网页或智能家居系统控制的设备;②构建支持学习、存储和远程触发红外指令的家庭自动化中枢;③实现空调、电视、音响等多品牌设备的统一集中控制;④拓展至语音控制、定时任务、传感器联动等高级应用场景。; 阅读建议:建议按照“环境搭建→信号学习→本地控制→联网控制→系统集成”的顺序逐步实践,重点关注红外收发电路设计、NVS数据保存机制与MQTT通信协议的对接,同时结合代码实例调试并优化发射功率与Wi-Fi稳定性。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧