博文
用委托 事件 异步 多线程 等方法写的一些程序 最好有注释 (2006-05-26 13:06:00)
摘要:using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 异步委托和线程调用
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
#region 默认代码
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 ......
].NET委托:一个C#睡前故事 [推荐](2006-05-26 13:04:00)
摘要:
.NET委托:一个C#睡前故事
英文版原作者:Chris Sells(www.sellsbrothers.com)
翻译:袁晓辉(www.farproc.com http://blog.csdn.net/uoyevoli)
紧耦合
从前,在南方一块奇异的土地上,有个工人名叫彼得,他非常勤奋,对他的老板总是百依百顺。但是他的老板是个吝啬的人,从不信任别人,坚决要求随时知道彼得的工作进度,以防止他偷懒。但是彼得又不想让老板呆在他的办公室里站在背后盯着他,于是就对老板做出承诺:无论何时,只要我的工作取得了一点进展我都会及时让你知道。彼得通过周期性地使用“带类型的引用”(原文为:“typed reference” 也就是delegate??)“回调”他的老板来实现他的承诺,如下:
class Worker {
public void Advise(Boss boss) { _boss = boss; }
public void DoWork() {
Console.WriteLine(“工作: 工作开始”);
if( _boss != null ) _boss.WorkStarted();
Console.WriteLine(“工作: 工作进行中”);
if( _boss != null ) _boss.WorkProgressing();
Console.WriteLine("“工作: 工作完成”");
if( _boss != null ) {
 ......
线程--借助 封装类 实现“线程调用带参方法”(示例下载) (2006-05-26 11:42:00)
摘要:(一).描述
由于线程只能执行无参数方法. 有时候需要线程执行"带参数方法"
此示例演示怎样借助封装类实现“线程调用带参方法”
(二).代码
using System;
using System.Threading;
namespace 借助封装类实现_线程调用带参方法_
{
class Help
{
public int x = 0; //乘数1
public int y = 0; //乘数2
public int end = 0; //存放结果
}
class MyClass
{
public static Help myHelp = new Help();
[STAThread]
static void Main(string[] args)
{
//给类的成员赋值
myHelp.x = 5;
myHelp.y = 10;
&nbs......
线程--管理线程(使线程中止,暂停,挂起等)(示例下载) (2006-05-26 11:42:00)
摘要:(一).描述
此示例演示怎样设置线程的状态(中止,暂停,挂起等)
(二).代码
using System;
using System.Threading;
namespace 管理线程_使线程中止_暂停_挂起等_
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
public static void Method1()
{
//thread1.Abort();一句中的 Abort会引发异常System.Threading.ThreadAbortException,其异常作用,下面会讲解
try
{
int i;
for(i=0;i<10;i++)
{
Console.WriteLine("Method1 at :" + i.ToString());
DelayTime(1); //延长时间(模拟执行任务)
}
}
catch(System.Threading.ThreadAbortException)
{
//注意一点,线程跳出此语句块后才终止。
//这里可以写释放......
线程--分别用lock以及Interlocked和Monitor类实现线程的临界(2006-05-26 11:41:00)
摘要:(一).描述
此示例演示分别用lock以及Interlocked和Monitor类实现线程的临界区操作(互斥)
(二).代码
using System;
using System.Threading;
using System.Collections;
namespace 加锁_实现临界区互斥操作_
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
private static ArrayList arrList = new ArrayList();
private static int i = 0;
public static void Add()
{
//方法一:用 lock 实现
// lock(arrList)
// {
// arrList.Add(i.ToString());
// i++;
// }
//方法二: 用Interlicked类实现
// System.Threading.Interlocked.Increment(ref i);
// arrList.Add(i.ToString());
//方法三: 用Monitor类实现
try
{
&......
线程--定制线程及设置和获取线程的优先级别(示例下载) (2006-05-26 11:40:00)
摘要:(一).描述
此示例演示怎样定制一个线程,并且设置线程的主要属性和获取线程运行时的状态
(二).代码
using System;
using System.Threading;
namespace 定制线程
{
//委托声明(函数签名)
//delegate string MyMethodDelegate();
class MyClass
{
public static void Method1()
{
int i;
for(i=0;i<10;i++)
{
Console.WriteLine("Method1 at :" + i.ToString());
//当线程停止/失败或未启动时IsAlive值为:false,否则为:true;
Console.WriteLine(" IsAlive is " + Thread.CurrentThread.IsAlive.ToString()+" ");
//是否是后台进程
Console.WriteLine(" IsBackGround is " + Thread.CurrentThread.IsBackground.ToString(......
线程--使用线程回调方法(示例下载) (2006-05-26 11:39:00)
摘要:(一).描述
此示例演示使用线程回调方法
(二).代码
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace 回调
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
//调用的方法
public static string MyMethod()
{
//Console.WriteLine(System.Threading.Thread.CurrentThread.IsBackground);
for(int i = 0;i < 3; i++) //延长时间(模拟实际任务)
{
Thread.Sleep(1000);
}
return "Hello Word";
}
//声明委托,调用MyMethod
private static MyMethodDelegate d = new MyMethodDelegate(MyClass.MyMethod);
//声明委托,调用AsyncCallbackMethod
private static System.AsyncCallback a = new System.AsyncCallback(MyClass.AsyncCallbackMetho......
线程--等待句柄(示例下载) (2006-05-26 11:39:00)
摘要:(一).描述
本示例代码实现线程等待等待执行,比如一个线程在执行之前要等待所有其它线程或某个线程
先执行完成,或者等待其它线程至少一个执行完成.
(二).代码
using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;
namespace 等待句柄
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
//要调用方法1
public string Write1()
{
for(double i = 0; i < 100000000000;i++) //此数值大小可以根据自己的环境修改,
//目的是让此方法延长时间而已
{
&n......
线程--通过委托异步调用方法(示例下载)(2006-05-26 11:37:00)
摘要:(一).描述
先运行个简单的线程示例,认识一下线程
通过委托调用方法,以及使用AsyncResult判断线程的状态
(二).代码
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace 通过委托异步调用方法
{
//委托声明(函数签名)
delegate string MyMethodDelegate();
class MyClass
{
//要调用的动态方法
public string MyMethod1()
{
return "Hello Word1";
}
//要调用的静态方法
public static string MyMethod2()
{
return "Hello Word2";
}
}
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
MyClass myClass = new MyClass();
//方式1: 声明委托,调......
线程--简述线程概述及原理 (2006-05-26 11:36:00)
摘要:线程是程序执行的基本原子单位. 一个进程可以由多个线程组成.
在分布式编程中,正确使用线程能够很好的提高应用程序的性能及运行效率.
实现原理是将一个进程分成多个线程,然后让它们并发异步执行,来提高运行效率.
并发执行并不是同时执行(占有CPU),任意时刻还是只能有一个线程占用CPU,
只不过是它们争夺CPU频繁一些,感觉到他们似乎都在运行.
下面通过一个简单的例子来说明一下它的工作原理
设一个进程要完成两个任务:任务1和任务2
并且任务1要经历: A1->B1->C1三个步骤才能完成
任务2要经历: A2->B2->C2三个步骤才能完成
I.如果两个任务同步执行的话完成两个任务是这样执行的:
花费时间段: 1 2 3 4 5 6
A1->B1->C1->A2->B2-C2
这样从A1一直到c2只能一个一个地执行. 当A1占用CPU执行时,从B1到C2线程只能在等待.
甚至它们不相互竞争同一个资源时,也要等待前面的执行完成,自己才能执行
II.如果两个任务异步执行的话完成两个任务是这样执行的:
花费时间段: 1 2 3 4 5 6
&nbs......