Office中国论坛/Access中国论坛
标题:
【原创】几种多线程参数传递方法
[打印本页]
作者:
faunus
时间:
2014-2-23 10:25
标题:
【原创】几种多线程参数传递方法
[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);
}
}
}
作者:
faunus
时间:
2014-2-23 10:26
[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);
}
}
}
作者:
faunus
时间:
2014-2-23 10:26
[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;
}
}
}
作者:
faunus
时间:
2014-2-23 10:26
[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);
}
}
}
作者:
faunus
时间:
2014-2-23 10:27
[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");
}
}
}
作者:
zpy2
时间:
2014-7-14 05:22
不错!还有线程同步也比较难!!!
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3