Hello I create 3 threads but I need that they use one array list in common to insert data , my question is that I create a thread like this Thread t = new Thread(doThread);
but if you see do thread it’s a method without parameters but i want to pass the array list mentioned before.
It´s possible ?
Asked
Active
Viewed 134 times
1

John Saunders
- 160,644
- 26
- 247
- 397

André De La O Campos
- 1,027
- 1
- 8
- 19
-
1Not exactly the same, but some good answers here: http://stackoverflow.com/questions/1360533/how-to-share-data-between-different-threads-in-c-sharp-using-aop – rsbarro Jul 01 '13 at 00:23
-
in fact what i really need to pass its a textbox, the arraylist and some strings. – André De La O Campos Jul 01 '13 at 00:30
-
2be careful, ArrayLists are not thread safe. – Matt Greer Jul 01 '13 at 00:59
-
2`ArrayList` is also obsolete. Use `List
` instead. – John Saunders Jul 01 '13 at 01:05 -
`List
` is also obsolete when working with multithreading, use something from the `System.Colections.Concurrent` namespace. – Scott Chamberlain Jul 01 '13 at 02:32
1 Answers
3
You can use a ParameterizedThreadStart
Delegate
For example,
ArrayList theList = new ArrayList();
Thread t = new Thread(doThread);
t.Start(theList);
This will work as long as your delegate, doThread
, has a matching signature of:
public delegate void ParameterizedThreadStart(
Object obj
)
More information about the ParameterizedStart
delegate can be found here.
Edit - just read that you will be needing more than an ArrayList
. Keep in mind that while it only accepts one parameter, you can create your own Object
as a wrapper for everything that you need to send to the method.
public class SendDataExample
{
public ArrayList myList { get; set; }
public string myString { get; set; }
}
You could then use the Object
in your delegate like this:
public void doThread(object data)
{
var sendDataExample = (SendDataExample)data;
ArrayList myList = sendDataExample.myList;
string myString = sendDataExample.myString;
...
}

Dan Teesdale
- 1,823
- 15
- 26
-
ok sounds great create the object, how use it in the method can you wirte a little example. Please – André De La O Campos Jul 01 '13 at 01:07