“掌握了委托和事件,才叫Windows程序设计入了门”。
using System;
using System.Collections.Generic;
using System.Text;
namespace MF
{
delegate void MulDoDelegate();
class Biz1
{
public int _count = 40;
public event MulDoDelegate MulDoing;
public event MulDoDelegate MulDoed;
public void Do()
{
for (int i = 0; i < _count; i++)
{
System.Threading.Thread.Sleep(50);
OnMulDo();
}
OnMulDoed();
}
public void OnMulDo()
{
if (MulDoing != null)
MulDoing();
}
public void OnMulDoed()
{
if (MulDoed != null)
MulDoed();
}
}
class Biz2
{
public int _count = 60;
public event MulDoDelegate MulDoing;
public event MulDoDelegate MulDoed;
public void Do()
{
for (int i = 0; i < _count; i++)
{
System.Threading.Thread.Sleep(50);
OnMulDo();
}
OnMulDoed();
}
public void OnMulDo()
{
if (MulDoing != null)
MulDoing();
}
public void OnMulDoed()
{
if (MulDoed != null)
MulDoed();
}
}
}
namespace MF
{
partial class Form2
{
/** <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);
}
Windows Form Designer generated code#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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(43, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(111, 23);
this.button1.TabIndex = 0;
this.button1.Text = "多线程调用操作";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(200, 88);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
/**//* *********************************************************
* “多线程调用方法,异步赋值到窗体中的控件”的Demo
*
* 作者:李中华
* 版权:浙江新能量科技有限公司
* 日期:2008-6-11
* *********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace MF
{
public partial class Form2 : Form
{
Form1 f;
//sdsklfjasdlkfjasklsdfdsfsdfsdfsd;
Thread t1;
Thread t2;
Biz1 b1;
Biz2 b2;
//进度条绑定的委托
MulDoDelegate mdd;
//业务类1操作完成后的提示的绑定的委托
MulDoDelegate mdd1_ed;
//业务类2操作完成后的提示的绑定的委托
MulDoDelegate mdd2_ed;
public Form2(Form1 f)
{
InitializeComponent();
this.f = f;
b1 = new Biz1();
b2 = new Biz2();
//业务类1的操作事件
b1.MulDoing += new MulDoDelegate(Biz_MulDoing);
//业务类2的操作事件
b2.MulDoing += new MulDoDelegate(Biz_MulDoing);
//业务类1的操作完成事件
b1.MulDoed += new MulDoDelegate(b1_MulDoed);
//业务类2的操作完成事件
b2.MulDoed += new MulDoDelegate(b2_MulDoed);
//委托绑定到方法
mdd = new MulDoDelegate(OnMdd_Add);
//委托绑定到方法
mdd1_ed = new MulDoDelegate(OnMdd1_Added);
//委托绑定到方法
mdd2_ed = new MulDoDelegate(OnMdd2_Added);
this.f.progressBar1.Minimum = 0;
this.f.progressBar1.Maximum = b1._count + b2._count;
}
void b2_MulDoed()
{
f.label1.Invoke(mdd2_ed);
}
void b1_MulDoed()
{
f.label1.Invoke(mdd1_ed);
}
void Biz_MulDoing()
{
f.progressBar1.Invoke(mdd);
}
void OnMdd_Add()
{
//激发进度条
if (f.progressBar1.Value < f.progressBar1.Maximum)
f.progressBar1.Value++;
}
void OnMdd1_Added()
{
//提示用户完成了调用
StringWriter sw = new StringWriter();
sw.WriteLine("完成了业务操作2");
f.label1.Text += sw.ToString();
//执行玩后终止线程
t1.Abort();
}
void OnMdd2_Added()
{
//提示用户完成了调用
StringWriter sw = new StringWriter();
sw.WriteLine("完成了业务操作1");
f.label1.Text += sw.ToString();
//执行玩后终止线程
t2.Abort();
}
private void button1_Click(object sender, EventArgs e)
{
this.f.progressBar1.Value = 0;
t1 = new Thread(new ThreadStart(b1.Do));
t2 = new Thread(new ThreadStart(b2.Do));
t1.Start();
t2.Start();
}
}
}
/**//* *********************************************************
* “多线程调用方法,异步赋值到窗体中的控件”的Demo
*
* 作者:李中华
* 版权:浙江新能量科技有限公司
* 日期:2008-6-11
* *********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace MF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.MdiParent = this;
f.WindowState = FormWindowState.Maximized;
f.Show();
}
}
}
namespace MF
{
partial class Form1
{
/** <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/** <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/** <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.label1 = new System.Windows.Forms.Label();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.子窗体ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.打开ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(0, 267);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(530, 23);
this.progressBar1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.Location = new System.Drawing.Point(0, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(0, 12);
this.label1.TabIndex = 2;
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.子窗体ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(530, 24);
this.menuStrip1.TabIndex = 6;
this.menuStrip1.Text = "menuStrip1";
//
// 子窗体ToolStripMenuItem
//
this.子窗体ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.打开ToolStripMenuItem});
this.子窗体ToolStripMenuItem.Name = "子窗体ToolStripMenuItem";
this.子窗体ToolStripMenuItem.Size = new System.Drawing.Size(53, 20);
this.子窗体ToolStripMenuItem.Text = "子窗体";
//
// 打开ToolStripMenuItem
//
this.打开ToolStripMenuItem.Name = "打开ToolStripMenuItem";
this.打开ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
this.打开ToolStripMenuItem.Text = "打开";
this.打开ToolStripMenuItem.Click += new System.EventHandler(this.打开ToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(530, 290);
this.Controls.Add(this.label1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.menuStrip1);
this.IsMdiContainer = true;
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public System.Windows.Forms.ProgressBar progressBar1;
public System.Windows.Forms.Label label1;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 子窗体ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 打开ToolStripMenuItem;
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MF
{
static class Program
{
/** <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}