找回密码
 注册
搜索
热搜: 回贴

一个关于委托、事件、多线程、异步的Demo

2010-1-30 22:12| 发布者: admin| 查看: 19| 评论: 0|原作者: 韩菱纱


Posted on 2008-06-22 13:49 李中华 阅读(74) 评论(1) 编辑 收藏
前些天帮同事解决一个多线程与控件调用的问题,我不想去理解他那复杂的业务,于是就写了这么一个Demo。
在开发Windows程序中,委托和事件用的很多,异步在某些场合下也会用到。曾经在用Dephi开发CRM软件的时候,我师傅对我说:“掌握了委托和事件,才叫Windows程序设计入了门”。可见委托和事件在Windows程序设计中的重要,其实,在ASP.NET服务端程序设计中,委托和事件也有非常广泛的用途,合理的使用,效果非常酷。因此,掌握这些东西,是每个想在程序路上发展的程序员必学的。

代码有简单的注释,废话就不多说了,要想看效果,做成Demo运行下即可。

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
{
/**////
/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

/**////
/// Clean up any resources being used.
///

/// true if managed resources should be disposed; otherwise, false.
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

/**////
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

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
{
/**////
/// 必需的设计器变量。
///

private System.ComponentModel.IContainer components = null;

/**////
/// 清理所有正在使用的资源。
///

/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**////
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///

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
{
/**////
/// 应用程序的主入口点。
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


李中华

最新评论

相关分类

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 15:20 , Processed in 0.166463 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部