读书人

线程多参数的传接方法除了类还有什么

发布时间: 2013-07-25 16:22:17 作者: rapoo

线程多参数的传递方法,除了类还有什么方法?
如题,哪位大神给出代码参考学习,不要类的方法



在线等待........
[解决办法]
用struct也可以。
[解决办法]
private void ThreadMethodWithParameter(object obj)
{
string[] arr = obj as string[];
if (arr != null)
{

}
}
调用

System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ThreadMethodWithParameter));
thread.Start(new string[] { "a", "b" });

给了你一个数组的例子
自己研究下吧
[解决办法]
匿名方法,闭包
[解决办法]
1:
int a = 1; int b = 0;
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(Th_proc));
th.Start(new object[] { a, b });

private void Th_proc(object args)
{
object[] objs = args as object[];
int aa = (int)objs[0];
int bb = (int)objs[1];
//...
}


2:
int a = 1; int b = 0;
th = new System.Threading.Thread((System.Threading.ThreadStart)delegate()


{
int aa = a;
int bb = b;
//...
});
th.Start();

3:
像楼上说的 将参数定义成class struct

有如此用法的还有
Control.BeginInvoke Socket.BeginSend/BeginReceive... Thread.Start

读书人网 >C#

热点推荐