如何创建多线程更新ProgressBar进度和利用lable控件来显示完成百分比

tong730 2008-11-21 05:06:24
如何创建多线程更新ProgressBar进度和利用lable控件来显示完成百分比 ProgressBar在工作时窗体不失效,可以自由拖动窗体,先谢过大家了
...全文
1054 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleve 2010-01-19
  • 打赏
  • 举报
回复
2楼的经典示范让我必须mark一下这个帖子
InsistOnDoing 2009-12-22
  • 打赏
  • 举报
回复
2楼写的很好,顶一个
全速前行 2008-11-24
  • 打赏
  • 举报
回复
学习了
up
tong730 2008-11-24
  • 打赏
  • 举报
回复
不知道为什么 窗体还是失效了 问题究竟在哪里?代码如下:
Public MyThread As System.Threading.Thread
Delegate Sub MyInvoke(ByVal Percent As Integer)
Dim ShowResult As New MyInvoke(AddressOf ShowInvoke)
Dim table As New DataTable

Private Sub 导出数据_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 导出数据.Click


strSelect = "Select 学校代码,学校,类型,院系,专业,班号,代码学号,学号,姓名,性别,电话,身份证号,毕业年份,拍摄标志,摄影师,日期,备注 from StudentTable where 学校='" + xx.Text + "' And 类型 = '" + lx.Text + "' And 拍摄标志='" + "1" + "'"

table = Editdata.Search(ConnStr, strSelect)
CysmManageSystem.TSPBar1.Maximum = table.Rows.Count
CysmManageSystem.TSPBar1.Visible = True
CysmManageSystem.TSSL1.Visible = True
Dim MyDelegate As New System.Threading.ThreadStart(AddressOf xxupdate1)
Dim MyThread As New System.Threading.Thread(MyDelegate)
MyThread.Start()

Private Sub xxupdate1()
Dim AcConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\data\Main.mdb"


Dim MyConnection As System.Data.OleDb.OleDbConnection = Nothing

MyConnection = New _
System.Data.OleDb.OleDbConnection(AcConnStr)
MyConnection.Open()


For i As Integer = 0 To table.Rows.Count - 1
标准的SQL插入语句 省略了
Dim MyCommand As System.Data.OleDb.OleDbCommand = New OleDbCommand(SQLString, MyConnection)
MyCommand.ExecuteNonQuery()
BeginInvoke(ShowResult, i)
Next
End Sub

Private Sub ShowInvoke(ByVal Percent As Integer)
Me.Label10.Text = Percent
me.TSPBar1.Value = Percent

End Sub

blues_zhao_yang 2008-11-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 tong730 的回复:]
非常感谢楼上的 但我用进度条检索数据库里的记录里 因数据库的记录不会正好是100条的 如何才能正确的显示百分比?
[/Quote]

诸如检索数据库这类程序,由于无法预先估计工作量,所以很难做到准确的控制任务进行的百分比,只能大致估计一下。

如果你只是想显示处理了多少条数据的话,可以用你检索的数据总量和100做一个比值,每处理完一定的数据百分比就+1

或者可以用进度条的ProgressBar.Style = ProgressBarStyle.Marquee样式来表示任务正在进行,不过这样无法让用户得知任务进度,而且在不支持此样式的操作系统中效果会消失。
wangjuenhui520 2008-11-21
  • 打赏
  • 举报
回复
我是初学者,提个建议,你应该先算出数据的数量. 如果你的progressbar 的单次移动数量是1 ,则除100,结果就是多少笔记录移动一格咯. 然后你的label 就= 你的进度条的value
tong730 2008-11-21
  • 打赏
  • 举报
回复
非常感谢楼上的 但我用进度条检索数据库里的记录里 因数据库的记录不会正好是100条的 如何才能正确的显示百分比?
一只熊猫 2008-11-21
  • 打赏
  • 举报
回复
简单的程序 用用 My.Application.DoEvents 就行了
blues_zhao_yang 2008-11-21
  • 打赏
  • 举报
回复
窗体:FormThreadInvoke
文本框:TextBoxPercentage
按钮:ButtonStart
ButtonStop
进度条:ProgressBarPercentage
blues_zhao_yang 2008-11-21
  • 打赏
  • 举报
回复

Dim MyThread As New System.Threading.Thread(AddressOf RunWork)
Delegate Sub MyInvoke(ByVal Percent As Integer)
Dim ShowResult As New MyInvoke(AddressOf ShowInvoke)

Private Sub FormThreadInvoke_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBarPercentage.Minimum = 0
ProgressBarPercentage.Maximum = 100
End Sub

Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
MyThread.Start()
End Sub

Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
MyThread.Abort()
End Sub

Private Sub RunWork()
For i = 0 To 100
BeginInvoke(ShowResult, i)
System.Threading.Thread.Sleep(100)
Next
End Sub

Private Sub ShowInvoke(ByVal Percent As Integer)
TextBoxPercentage.Text = Percent & "%"
ProgressBarPercentage.Value = Percent
If Percent = 100 Then
MsgBox("任务完成!")
MyThread.Abort()
End If
End Sub
tong730 2008-11-21
  • 打赏
  • 举报
回复
晕 C的代码不熟 能不能转换成VB.NET的呀 谢谢了
android2008 2008-11-21
  • 打赏
  • 举报
回复
帮顶
filec75 2008-11-21
  • 打赏
  • 举报
回复
友情up
tinghai_xu 2008-11-21
  • 打赏
  • 举报
回复
或者考虑使用 BackgroundWorker 组件也可以
tinghai_xu 2008-11-21
  • 打赏
  • 举报
回复
请参考:

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;
using System.Threading;

namespace TestWinForm
{
public partial class Form1 : Form
{
delegate void SetValueCallback(int value);

public Form1()
{
InitializeComponent();
}

private void btnRun_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(Foo));
t.Start();
}

private void Foo()
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
SetProcessBarValue(i);
SetLabelValue(i);
}
}

private void SetLabelValue(int value)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.lblStatus.InvokeRequired)
{
SetValueCallback d = new SetValueCallback(SetLabelValue);
this.Invoke(d, new object[] { value });
}
else
{
this.lblStatus.Text = value.ToString()+'%';
}
}

private void SetProcessBarValue(int value)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.prbStatus.InvokeRequired)
{
SetValueCallback d = new SetValueCallback(SetProcessBarValue);
this.Invoke(d, new object[] { value });
}
else
{
this.prbStatus.Value = value;
}
}
}
}


namespace TestWinForm
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.prbStatus = new System.Windows.Forms.ProgressBar();
this.btnRun = new System.Windows.Forms.Button();
this.lblStatus = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// prbStatus
//
this.prbStatus.Location = new System.Drawing.Point(43, 44);
this.prbStatus.Name = "prbStatus";
this.prbStatus.Size = new System.Drawing.Size(296, 23);
this.prbStatus.TabIndex = 0;
//
// btnRun
//
this.btnRun.Location = new System.Drawing.Point(190, 99);
this.btnRun.Name = "btnRun";
this.btnRun.Size = new System.Drawing.Size(75, 23);
this.btnRun.TabIndex = 1;
this.btnRun.Text = "Run";
this.btnRun.UseVisualStyleBackColor = true;
this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
//
// lblStatus
//
this.lblStatus.AutoSize = true;
this.lblStatus.Location = new System.Drawing.Point(367, 54);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(0, 13);
this.lblStatus.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(496, 266);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.btnRun);
this.Controls.Add(this.prbStatus);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.ProgressBar prbStatus;
private System.Windows.Forms.Button btnRun;
private System.Windows.Forms.Label lblStatus;
}
}

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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