Reputation: 1047
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 ?
Upvotes: 1
Views: 136
Reputation: 1863
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;
...
}
Upvotes: 3