16,555
社区成员
发帖
与我相关
我的任务
分享
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
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;
}
}