📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace DelegateAsyncRun
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button _btnRun;
private System.Windows.Forms.NumericUpDown _txtSecond;
private System.Windows.Forms.Label _lblSecond;
private System.Windows.Forms.ProgressBar _Progress;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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._btnRun = new System.Windows.Forms.Button();
this._txtSecond = new System.Windows.Forms.NumericUpDown();
this._lblSecond = new System.Windows.Forms.Label();
this._Progress = new System.Windows.Forms.ProgressBar();
((System.ComponentModel.ISupportInitialize)(this._txtSecond)).BeginInit();
this.SuspendLayout();
//
// _btnRun
//
this._btnRun.Location = new System.Drawing.Point(195, 16);
this._btnRun.Name = "_btnRun";
this._btnRun.TabIndex = 2;
this._btnRun.Text = "执行";
this._btnRun.Click += new System.EventHandler(this._btnRun_Click);
//
// _txtSecond
//
this._txtSecond.Location = new System.Drawing.Point(107, 17);
this._txtSecond.Maximum = new System.Decimal(new int[] {
10000,
0,
0,
0});
this._txtSecond.Name = "_txtSecond";
this._txtSecond.Size = new System.Drawing.Size(72, 20);
this._txtSecond.TabIndex = 1;
this._txtSecond.Value = new System.Decimal(new int[] {
1,
0,
0,
0});
//
// _lblSecond
//
this._lblSecond.Location = new System.Drawing.Point(19, 16);
this._lblSecond.Name = "_lblSecond";
this._lblSecond.Size = new System.Drawing.Size(64, 23);
this._lblSecond.TabIndex = 0;
this._lblSecond.Text = "执行秒数";
//
// _Progress
//
this._Progress.Dock = System.Windows.Forms.DockStyle.Bottom;
this._Progress.Location = new System.Drawing.Point(0, 135);
this._Progress.Maximum = 1;
this._Progress.Name = "_Progress";
this._Progress.Size = new System.Drawing.Size(288, 23);
this._Progress.TabIndex = 3;
//
// Form1
//
this.AcceptButton = this._btnRun;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(288, 158);
this.Controls.Add(this._Progress);
this.Controls.Add(this._lblSecond);
this.Controls.Add(this._txtSecond);
this.Controls.Add(this._btnRun);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "委托异步方式执行";
((System.ComponentModel.ISupportInitialize)(this._txtSecond)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new Form1() );
}
// 显示进度条
void ShowProgress( int totalStep, int currentStep )
{
_Progress.Maximum = totalStep;
_Progress.Value = currentStep;
}
// 执行任务的委托声明
delegate void RunTaskDelegate( int seconds );
// 执行任务
void RunTask( int seconds )
{
// 每 1 / 4 秒 显示进度一次
for( int i = 0; i < seconds * 4; i++ )
{
Thread.Sleep( 250 );
// 显示进度条
ShowProgress( seconds * 4, i + 1 );
}
}
// 通过创建工作线程消除用户界面线程的阻塞问题
private void _btnRun_Click( object sender, System.EventArgs e )
{
RunTaskDelegate runTask = new RunTaskDelegate( RunTask );
// 委托同步调用方式
//runTask( Convert.ToInt16( _txtSecond.Value ) );
// 委托异步调用方式
runTask.BeginInvoke( Convert.ToInt16( _txtSecond.Value ), null, null );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -