I’m kinda new to Multi-Threading and have only played around with it in the past. But I’m curious if it is possible to have a List of byte arrays on a main thread and still be able to add to that List while creating the new byte array in a seperate Thread. Also, I’ll be using a for-each loop that will go through a list of forms that will be used to parse into the byte array. So basically a pseudo code would be like this…
reports = new List();
foreach (form in forms)
{
newReport = new Thread(ParseForm(form));
reports.Add(newReport);
}
void ParseForm(form)
{
newArray = new byte[];
newArray = Convert.ToBytes(form);
return newArray;
}
Hopefully the pseudo-code above makes some sense. If anyone could tell me if this is possible and point me in the direction of a good example, I’m sure I can figure out the actual code.
If you need to access a collection from multiple threads, you should either use synchronization, or use a
SynchronizedCollectionif your .NET version is 3.0 or higher.Here is one way to make the collection accessible to your thread:
If you are on .NET 4 or later, a much better alternative to managing your threads manually is presented by various classes of the
System.Threading.Tasksnamespace. Consider exploring this alternative before deciding on your threading implementation.