设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 7627|回复: 5
打印 上一主题 下一主题

【原创】几种多线程参数传递方法

[复制链接]
跳转到指定楼层
1#
发表于 2014-2-23 10:25:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[1]通过构造函数传递


  • using System;  
  • using System.Threading;  
  • using System.Runtime.Remoting.Messaging;  
  • namespace PCTools  
  • {  
  •     class Test  
  •     {  
  •         static void Main(string[] args)  
  •         {  
  •             Console.WriteLine("========Test Begin=========");  
  •             //Create   
  •             B b = new B("[1]构造函数传递法!");  
  •             //Start  
  •             new Thread(b.Func).Start();  
  •             Console.WriteLine("===========Test End=========");  
  •             Console.ReadKey();  
  •         }  
  •     }  
  •     class B  
  •     {  
  •         private string s;  
  •         public B(string s)  
  •         {  
  •             this.s = s;  
  •         }  
  •         public void Func()  
  •         {  
  •             Console.WriteLine(s);  
  •         }  
  •     }  
  • }  





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2014-2-23 10:26:15 | 只看该作者
[2]线程池传递参数法


  • using System;  
  • using System.Threading;  
  • using System.Runtime.Remoting.Messaging;  
  • namespace PCTools  
  • {  
  •     class Test  
  •     {  
  •         static void Main(string[] args)  
  •         {  
  •             Console.WriteLine("========Test Begin=========");  
  •             //Create   
  •             B b = new B();  
  •             //Start  
  •             ThreadPool.QueueUserWorkItem(new WaitCallback(b.Func), "[2]线程池传递参数法");  
  •             Console.WriteLine("===========Test End=========");  
  •             Console.ReadKey();  
  •         }  
  •     }  
  •     class B  
  •     {  
  •         public void Func(string s)  
  •         {  
  •             Console.WriteLine(s);  
  •         }  
  •         public void Func(object s)  
  •         {  
  •             this.Func((string)s);  
  •         }  
  •     }  
  • }  





3#
 楼主| 发表于 2014-2-23 10:26:36 | 只看该作者

[3]回调法,并且能够处理返回值

//注意:CallBack必须是static的


  • using System;  
  • using System.Threading;  
  • using System.Runtime.Remoting.Messaging;  
  • namespace PCTools  
  • {  
  •     //delegate   
  •     class Test  
  •     {  
  •         delegate string MyMethodDelegate(string s);  
  •         static void Main(string[] args)  
  •         {  
  •             Console.WriteLine("========Test Begin=========");  
  •             //Create   
  •             B b = new B();  
  •             //Start  
  •             MyMethodDelegate MyMethod = b.Func;  
  •             MyMethod.BeginInvoke("[3]回调法,并且能够处理返回值", CallBack, null);  
  •             Console.WriteLine("===========Test End=========");  
  •             Console.ReadKey();  
  •         }  
  •         static public void CallBack(IAsyncResult result)  
  •         {  
  •             AsyncResult async = (AsyncResult)result;  
  •             MyMethodDelegate DelegateInstance = (MyMethodDelegate)async.AsyncDelegate;  
  •             Console.WriteLine(DelegateInstance.EndInvoke(result));  
  •         }  
  •     }  
  •     class B  
  •     {  
  •         public string Func(string s)  
  •         {  
  •             return s;  
  •         }  
  •     }  
  • }  





4#
 楼主| 发表于 2014-2-23 10:26:57 | 只看该作者
[4]回调法,并且能够处理返回值","+多参数版"


  • using System;  
  • using System.Threading;  
  • using System.Runtime.Remoting.Messaging;  
  • namespace PCTools  
  • {  
  •     //delegate   
  •     class Test  
  •     {  
  •         delegate string MyMethodDelegate(string s ,string t);  
  •         static void Main(string[] args)  
  •         {  
  •             Console.WriteLine("========Test Begin=========");  
  •             //Create   
  •             B b = new B();  
  •             //Start  
  •             MyMethodDelegate MyMethod = b.Func;  
  •             MyMethod.BeginInvoke("[5]回调法,并且能够处理返回值","+多参数版", CallBack, null);  
  •             //方法五  
  •             Thread fThread = new Thread(b.Func);  
  •             fThread.Start("[4]处理单个参数");  
  •             Console.WriteLine("===========Test End=========");  
  •             Console.ReadKey();  
  •         }  
  •         static public void CallBack(IAsyncResult result)  
  •         {  
  •             AsyncResult async = (AsyncResult)result;  
  •             MyMethodDelegate DelegateInstance = (MyMethodDelegate)async.AsyncDelegate;  
  •             Console.WriteLine(DelegateInstance.EndInvoke(result));  
  •         }  
  •     }  
  •     class B  
  •     {  
  •         public string Func(string s, string t)  
  •         {  
  •             return s + t;  
  •         }  
  •         public void Func(string s)  
  •         {  
  •             Console.WriteLine(s);  
  •         }  
  •         public void Func(object s)  
  •         {  
  •             this.Func((string)s);  
  •         }  
  •     }  
  • }  





5#
 楼主| 发表于 2014-2-23 10:27:18 | 只看该作者
[5]最后来个简单点的


  • using System.Threading;  
  • namespace ConsoleApplication2  
  • {  
  •     class Program  
  •     {  
  •         static void Main(string[] args)  
  •         {  
  •             Thread fThread = new Thread(new ParameterizedThreadStart(B.Func));  
  •             fThread.Start(2);  
  •         }  
  •     }  
  •     class B  
  •     {  
  •         public static void Func(object a)  
  •         {  
  •             int i = (int)a;  
  •             Console.WriteLine("Smile");  
  •         }  
  •     }  
  • }  





点击这里给我发消息

6#
发表于 2014-7-14 05:22:48 来自手机 | 只看该作者
不错!还有线程同步也比较难!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-24 11:43 , Processed in 0.103267 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表