如何动态现实进度

precipitant 2003-10-15 05:44:52
这样的程序,我在另外一个表(a)中的一个字段(bzh)的值覆盖到另外一个表(b)中的某个字段(stand_text),用数据表读取a表,用视图(dv)检索,然后循环中执行update语句更新,每更新一条我都想将进度显示出来,但是我的没有进度显示,执行时什么都看不到,执行完了(可能花了15秒钟)直接显示100了,不知道这种情况是怎么回事?

for(int i=0;i<dv.Count;i++)
{
theField=dv[i]["bzh"].ToString().Trim();
i_code=dv[i]["bh"].ToString().Trim();
UpdateStr="update epd_item_master set stand_text='" + theField + "' where item_code='" + i_code + "'";
textBox2.Text+=i_code + "---" + theField + "\r\n";
ocom.CommandText=UpdateStr;
ocom.CommandType=CommandType.Text;
ocom.ExecuteNonQuery();
tCount++;
label2.Text=i_code + " , " + theField + " " + ((tCount * 100) /dv.Count).ToString() + "% 被处理!";

}
...全文
25 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoobj 2003-10-16
  • 打赏
  • 举报
回复
(1)微软提供的可以解决你的问题。
namespace Microsoft.Samples.Windows.Forms.Cs.ProgressBarCtl {
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

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

private System.ComponentModel.Container components;
protected internal System.Windows.Forms.Label label3;
protected internal System.Windows.Forms.Label lblCompleted;
protected internal System.Windows.Forms.TrackBar sldrSpeed;
protected internal System.Windows.Forms.ProgressBar progbar;
protected internal System.Windows.Forms.Label label5;
protected internal System.Windows.Forms.GroupBox grpBehavior;
protected internal System.Windows.Forms.Label label4;
protected internal System.Windows.Forms.Label label6;
protected internal System.Windows.Forms.Label lblValue;
protected internal System.Windows.Forms.ComboBox cmbStep;

private int iSleepTime ;
private Thread timedProgress ;

public ProgressBarCtl() : base() {
//
// Win 窗体设计器支持所必需的
//
InitializeComponent();

iSleepTime = 100 ;
cmbStep.SelectedIndex = 0 ;
progbar.Step = 1 ;

}

protected override void OnLoad(EventArgs e) {
// 产生一个新的线程来更新 ProgressBar 控件
timedProgress = new Thread(new ThreadStart(TimedProgressProc));
timedProgress.IsBackground = true;
timedProgress.Start();
}

// <doc>
// <desc>
// 此代码在 Windows.Forms 线程上执行。
// </desc>
// </doc>
//
private void UpdateProgress() {
int min ;
double numerator, denominator, completed ;

//如果需要的话,进行重置以开始
if (progbar.Value == progbar.Maximum) {
progbar.Value = progbar.Minimum ;
}
else {
progbar.PerformStep();
}

lblValue.Text = progbar.Value.ToString();

min = progbar.Minimum ;
numerator = progbar.Value - min ;
denominator = progbar.Maximum - min ;
completed = (numerator / denominator) * 100.0 ;

lblCompleted.Text = Math.Round(completed).ToString() + "%" ;
}

// <doc>
// <desc>
// 此函数在 timedProgress 线程中运行,
// 更新窗体上的 ProgressBar。
// </desc>
// </doc>
//
private void TimedProgressProc() {
try {
MethodInvoker mi = new MethodInvoker(UpdateProgress);
while (true) {
Invoke(mi);
int iSleepTime = this.SleepTime;
Thread.Sleep(iSleepTime) ;
}
}
//当该线程被主线程中断时引发 - 退出本循环
catch (ThreadInterruptedException e) {
if (e != null) {}
}
catch (Exception we) {
if (we != null) {
MessageBox.Show(we.ToString());
}
}
}

// <doc>
// <desc>
// 控制进度栏的进度的属性 - 由后台线程使用
// </desc>
// </doc>
//
private int SleepTime {
get {
lock(this) {
return iSleepTime ;
}
}
set {
lock(this) {
iSleepTime = value ;
}
}
}

/// <summary>
/// 清理正在使用的所有资源
/// </summary>
public new void Dispose() {
/*
* 必须确保在处置了该线程之后,该线程
* 不会尝试访问我们的控件。
*/
if (timedProgress != null) {
timedProgress.Interrupt();
timedProgress = null;
}

base.Dispose();
components.Dispose();
}
yoobj 2003-10-16
  • 打赏
  • 举报
回复
(2)

protected void sldrSpeed_Scroll(object sender, EventArgs e) {
TrackBar tb = (TrackBar) sender ;
int time = 110 - tb.Value ;
this.SleepTime = time ;
}

protected void cmbStep_SelectedIndexChanged(object sender, EventArgs e) {
try {
progbar.Step = Int32.Parse((string)cmbStep.SelectedItem);
}
catch (Exception ex) {
// 如果 Int32.Parse 不能转换则引发
if (ex !=null) {}
}
}


/// <summary>
/// 设计器支持所必需的方法 - 不要使用
/// 代码编辑器修改此方法的内容
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
this.label4 = new System.Windows.Forms.Label();
this.lblCompleted = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.lblValue = new System.Windows.Forms.Label();
this.cmbStep = new System.Windows.Forms.ComboBox();
this.progbar = new System.Windows.Forms.ProgressBar();
this.sldrSpeed = new System.Windows.Forms.TrackBar();
this.label6 = new System.Windows.Forms.Label();
this.grpBehavior = new System.Windows.Forms.GroupBox();

sldrSpeed.BeginInit();

this.Text = "ProgressBar";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.ClientSize = new System.Drawing.Size(506, 175);
this.MaximizeBox = false;
this.MinimizeBox = false;

label4.Location = new System.Drawing.Point(16, 80);
label4.Text = "完成速度:";
label4.Size = new System.Drawing.Size(225, 16);
label4.TabIndex = 0;

lblCompleted.Location = new System.Drawing.Point(128, 56);
lblCompleted.Size = new System.Drawing.Size(56, 16);
lblCompleted.TabIndex = 2;

label5.Location = new System.Drawing.Point(24, 56);
label5.Text = "已完成的百分比:";
label5.Size = new System.Drawing.Size(112, 24);
label5.TabIndex = 1;

label3.Location = new System.Drawing.Point(16, 24);
label3.Text = "步长:";
label3.Size = new System.Drawing.Size(48, 16);
label3.TabIndex = 6;

lblValue.Location = new System.Drawing.Point(128, 80);
lblValue.Size = new System.Drawing.Size(56, 16);
lblValue.TabIndex = 4;

cmbStep.Location = new System.Drawing.Point(136, 24);
cmbStep.Size = new System.Drawing.Size(96, 21);
cmbStep.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmbStep.TabIndex = 7;
cmbStep.SelectedIndexChanged += new System.EventHandler(cmbStep_SelectedIndexChanged);
cmbStep.Items.AddRange(new object[] {"1",
"5",
"10",
"20"});

progbar.BackColor = System.Drawing.SystemColors.Control;
progbar.Location = new System.Drawing.Point(24, 24);
progbar.TabIndex = 0;
progbar.Size = new System.Drawing.Size(192, 16);
progbar.Step = 1;
progbar.Text = "progbar";

sldrSpeed.BackColor = System.Drawing.SystemColors.Control;
sldrSpeed.TickFrequency = 10;
sldrSpeed.Location = new System.Drawing.Point(16, 96);
sldrSpeed.TabIndex = 1;
sldrSpeed.TabStop = false;
sldrSpeed.Value = 10;
sldrSpeed.Maximum = 100;
sldrSpeed.Size = new System.Drawing.Size(216, 42);
sldrSpeed.Text = "trackBar1";
sldrSpeed.Minimum = 10;
sldrSpeed.Scroll += new System.EventHandler(sldrSpeed_Scroll);

label6.Location = new System.Drawing.Point(24, 80);
label6.Text = "值:";
label6.Size = new System.Drawing.Size(100, 16);
label6.TabIndex = 3;

grpBehavior.Location = new System.Drawing.Point(248, 16);
grpBehavior.TabIndex = 5;
grpBehavior.TabStop = false;
grpBehavior.Text = "ProgressBar";
grpBehavior.Size = new System.Drawing.Size(248, 152);
grpBehavior.Controls.Add(cmbStep);
grpBehavior.Controls.Add(label3);
grpBehavior.Controls.Add(sldrSpeed);
grpBehavior.Controls.Add(label4);
this.Controls.Add(grpBehavior);
this.Controls.Add(lblValue);
this.Controls.Add(label6);
this.Controls.Add(lblCompleted);
this.Controls.Add(label5);
this.Controls.Add(progbar);

sldrSpeed.EndInit();
}


// 应用程序的主入口点。
[STAThread]
public static void Main(string[] args) {
Application.Run(new ProgressBarCtl());
}

}

}
precipitant 2003-10-16
  • 打赏
  • 举报
回复
能够举出一个实例
xiaodele 2003-10-16
  • 打赏
  • 举报
回复
确实如此,想要让Label中有东西显示可以用另外一个线程来处理或者委托,但是委托没有试过,用另外的线程肯定可以
precipitant 2003-10-16
  • 打赏
  • 举报
回复
就是根据
label2.Text=i_code + " , " + theField + " " + ((tCount * 100) /dv.Count).ToString() + "% 被处理!";

将当前的处理进度显示在一个label2中,如何实现。

现在的情况时在进行处理的时候,label2中什么显示都没有,只是在全处理完了以后显示100%被处理。。。

feigehao 2003-10-15
  • 打赏
  • 举报
回复
up
寒星 2003-10-15
  • 打赏
  • 举报
回复
如果你使用的是SQL SERVER2000的话,可以考虑调用SQLDMO,它可以捕获到数据库记录被更新的事件。SQL SERVER2000自带了SQLDMO相关的例子。

110,533

社区成员

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

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

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