c# multi thread programming ,c# 多线程编程

使用函数代表(委托)的BeginInvoke()和EndInvoke()方法。可以在新开的线程上运行函数,并且在主线程得到函数的返回值。

using System;
using System.Threading;

namespace MultiThread
{
public delegate int Handler (int a);//use of Handler.BeginInvoke() and EndInvoke() methods,
								//first,we need a handler.
class F     	 {
	public static int Accum(int a)//this function should run in child thread.
		{
		Console.WriteLine("this is in child thread");
		Console.WriteLine("begin accumulation in child thread");
		int i,n;
		n = 10;
		//simulate the time consuming,seemingly,the accumulation takes some time.
		for(i=1;i<n;i++)
			{
			a ++;
			Console.WriteLine("now the number reaches {0}",a);
			Thread.Sleep(1000);
			}
		
		return a;
		}
	public static int Add(int a,int b)//the function should run in main thread.
		{
		return a+b;
		}
				 }
class Program{
	public static void Main(string[] args)
		{
		Console.WriteLine("this is in main thread");//main thread begins.
		Handler myHandler = new Handler(F.Accum);
		IAsyncResult result = myHandler.BeginInvoke(2,null,null);//child thread runs F.Accum() beginning.
																//child thread is another thread.
															   //main thread continues following execution.
		Console.WriteLine("main thread executes some implementations that consume several seconds");
		
		int i,n,d;
		n = 10;
		d = 0;//the d
		//some implementations consumes some time.
		for(i=1;i<n;i++)
			{Thread.Sleep(1000);
			 d += i;}//the d
		Console.WriteLine("the implementationin in main thread completed,consuming {0} seconds",i);
		//get the result of child thread and do it an addition.
		int r1 = myHandler.EndInvoke(result);//get the result of thread if it ends.
		Console.WriteLine("the final number returned to main thread is  {0}",r1);
		Console.WriteLine("now the final number will be added a digit  {0} in main thread",d);//the d
		int r2;
		r2 = F.Add(r1,d);
		Console.WriteLine("the result after addition is {0}",r2);
		
		Console.Write("Press any key to continue . . . ");
		Console.ReadKey(true);
		}
			}
}
在这段代码中,可以看出,当主线程在执行一些耗时的代码的时候。子线程的工作仍在同时进行。这样的并发同时运行两段代码,与单线程执行完一段代码再执行一段代码相比,就节约了很多时间。在主线程得到了子 线程运行函数的结果以后,再进行利用,给它加上一个数。演示了主线程如何得到子线程的运行结果并利用。

下面我用单线程的模式运行上面的代码,显然耗时得多。

using System;
using System.Threading;

namespace MultiThread2
{
public delegate int Handler (int a);//use of Handler.Invoke()  methods,
								//first,we need a handler.
class F     	 {
	public static int Accum(int a)
		{
		Console.WriteLine("this is in single main  thread");
		Console.WriteLine("begin accumulation");
		int i,n;
		n = 10;
		//simulate the time consuming,seemingly,the accumulation takes some time.
		for(i=1;i<n;i++)
			{
			a ++;
			Console.WriteLine("now the number reaches {0}",a);
			Thread.Sleep(1000);
			}
		
		return a;
		}
	public static int Add(int a,int b)
		{
		return a+b;
		}
				 }
class Program{
	public static void Main(string[] args)
		{
		Console.WriteLine("this is in main thread");
		Handler myHandler = new Handler(F.Accum);
		int result = myHandler.Invoke(2);
		Console.WriteLine("the final number returned to main thread is  {0}",result);
		Console.WriteLine("main thread executes some implementations that consume several seconds");
		
		int i,n,d;
		n = 10;
		d = 0;//the d
		//some implementations consumes some time.
		for(i=1;i<n;i++)
			{Thread.Sleep(1000);
			 d += i;}//the d
		Console.WriteLine("the implementationin in main thread completed,consuming {0} seconds",i);

		Console.WriteLine("now the final number will be added a digit  {0} in main thread",d);//the d
		int r2;
		r2 = F.Add(result,d);
		Console.WriteLine("the result after addition is {0}",r2);
		
		Console.Write("Press any key to continue . . . ");
		Console.ReadKey(true);
		}
			}
}

下面是双线程和单线程执行同样的程式耗时对比



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值