Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 线程方法参数_C#_Multithreading - Fatal编程技术网

C# 线程方法参数

C# 线程方法参数,c#,multithreading,C#,Multithreading,您好,我创建了3个线程,但我需要它们共同使用一个数组列表来插入数据,我的问题是我创建了这样一个线程thread t=new thread(doThread)但是如果你看到do thread,它是一个没有参数的方法,但是我想传递前面提到的数组列表。 有可能吗?您可以使用参数化线程启动委托 比如说, ArrayList theList = new ArrayList(); Thread t = new Thread(doThread); t.Start(theList); 只要您的委托,doTh

您好,我创建了3个线程,但我需要它们共同使用一个数组列表来插入数据,我的问题是我创建了这样一个线程
thread t=new thread(doThread)
但是如果你看到do thread,它是一个没有参数的方法,但是我想传递前面提到的数组列表。
有可能吗?

您可以使用
参数化线程启动
委托

比如说,

ArrayList theList = new ArrayList(); 
Thread t = new Thread(doThread);
t.Start(theList);
只要您的委托,
doThread
,具有以下匹配签名,这将起作用:

public delegate void ParameterizedThreadStart(
    Object obj
)
有关
ParameterizedStart
委托的更多信息,请参见

编辑-只需阅读您需要的不仅仅是
ArrayList
。请记住,虽然它只接受一个参数,但您可以创建自己的
对象
,作为需要发送到该方法的所有内容的包装器

public class SendDataExample
{
   public ArrayList myList { get; set; }
   public string myString { get; set; }
}
然后可以在代理中使用
对象
,如下所示:

public void doThread(object data)
{
    var sendDataExample = (SendDataExample)data;
    ArrayList myList = sendDataExample.myList;
    string myString = sendDataExample.myString;
    ...
}

不完全相同,但这里有一些很好的答案:事实上,我真正需要传递的是一个文本框、arraylist和一些字符串。小心,arraylist不是线程安全的。
arraylist
也过时了。改为使用
List
List
在处理多线程时也会过时,请使用
System.Colections.Concurrent
命名空间中的内容。好的,听起来不错。创建对象时,如何在方法中使用它?您可以编写一个小示例。请