首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2013-07-25 
线程多参数的传递方法,除了类还有什么方法?如题,哪位大神给出代码参考学习,不要类的方法在线等待........[

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



在线等待........
[解决办法]
用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

热点排行