The code below is simplified. There is a massive method which iterates many times. The threaded method receives an element of a List<Class>
.
Because the threaded method modifies an element which is an object in the method, I do not want that the same argument is loaded on the separate threads concurrently. Because each element is independent, I want that the method with each one runs concurrently.
How to run the method with the same argument sequentially and run the method with a different argument concurrently?
Do I have to verify each running thread one by one before New & Start the method, whether there is the method with the same argument or not?
class Class1
{
// something
}
private void Form1_Load(object sender, EventArgs e)
{
List<Class1> _List = new();
for (int i = 0; i < 10; i++)
{
_List.Add(new Class1 { });
}
for (int i = 0; i < 10; i++)
{
Thread _Thread = new(Method1);
_Thread.Start(_List[new Random().Next(10)]); // the argument can be consecutively same element of List1
}
}
void Method1(object _Object)
{
// modifies _Object
}