Multithreading and passing data to new threads <p>I have some issues with an application I'm coding. It's job is to solve a maze using threads. One thread begins, and for each bifurcation it calls a static method in another class, passing parameters that the other thread needs, and then starts threads for each path. My outputs are all messed up, and I'm not sure if it's a Multithreading problem or maybe a problem with the references. Here's some of the code (Each thread is given a new instance of an <code>Explorer</code> class):</p> <p>This is the <code>Run()</code> method for each thread:</p> <pre><code>public void Explore() { while (ImDone == false) { Move(); if (ActualPosition[0] != Labyrinth.ExitPoint[0] || ActualPosition[1] != Labyrinth.ExitPoint[1]) //I'm not at the end.. continue; PrintMyStatus(); //Print in the console my parents and my complete route.. break; } </code></pre> <p>This is the <code>Move()</code> method:</p> <pre><code>List&lt;int[]&gt; validPaths = CheckSurroundings(); //returns a list of paths switch (validPaths.Count) { case 0: ImDone = true; //No more routes available break; case 1: MarkMyPosition(validPaths); //Change internal variables to the only new path break; default: lock(this) //As this involves thread creating I locked it.. { //Creating deep copies of my "History" so my childs can have them.. List&lt;int[]&gt; DCValidPaths = DeepCopy(validPaths); List&lt;string&gt; DCMyParents = DeepCopy(MyParents); string[,] DCMyExplorationMap = DeepCopy(MyExplorationMap); List&lt;int[]&gt; DCMyPositions = DeepCopy(MyPositions); foreach (var path in validPaths) { DCMyParents.Add(Thread.CurrentThread.Name); //Adding myself as a parent ExplorationManager.ExplorerMaker( DCValidPaths, DCMyParents, DCMyExplorationMap, DCMyPositions, ID); //Threads are created in the static class } } break; } </code></pre> <p>My <code>DeepCopy()</code> method is using is the one in the accepted answer of <a href="http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically">this</a> question. Any other info needed I'll be more than happy to provide in order to solve the problem. Thanks in advance for your help.</p> <p>EDITS: My problem consists basically in: I'm having an <code>OutOfMemoryException</code> in the <code>Thread.Start()</code> clause and the Path displayed by the threads contain corrupted data (incorrect positions). I tested the <code>CheckSurroundings()</code> method and by far only returns correct positions (The labyrinth is contained in a two-dimensional string array).</p> <p>This is the constructor of the <code>Explorer</code> class:</p> <pre><code>public Explorer(List&lt;string&gt; myParents, int[] actualPosition, string[,] myExplorationMap, List&lt;int[]&gt; myPositions, int ID) { //Here I pass the data specified in Move(); MyParents = myParents; ActualPosition = actualPosition; MyExplorationMap = myExplorationMap; MyPositions = myPositions; this.ID = ID + 1; //An ID for reference //Marking position in my map with "1" so I know I've been here already, //and adding the position given to my list of positions MyExplorationMap[ActualPosition[0], ActualPosition[1]] = "1"; MyPositions.Add(DeepCopy(ActualPosition)); } </code></pre> <p>And this is the class creating the threads:</p> <pre><code>public static class ExplorationManager { public static void ExplorerMaker(List&lt;int[]&gt; validPaths, List&lt;string&gt; myParents, string[,] myExplorationMap, List&lt;int[]&gt; myPositions, int ID) { foreach (var thread in validPaths.Select (path =&gt; new Explorer(myParents, path, myExplorationMap, myPositions,ID)). Select(explorer =&gt; new Thread(explorer.Explore))) { thread.Name = "Thread of " + ID + " generation"; thread.Start(); //For each Path in Valid paths, create a new instance of Explorer and assign a thread to it. } } } </code></pre> <p>And the method returning the ValidPaths</p> <pre><code> private List&lt;int[]&gt; CheckSurroundings() { var validPaths = new List&lt;int[]&gt;(); var posX = ActualPosition[0]; var posY = ActualPosition[1]; for (var dx = -1; dx &lt;= 1; dx++) { if (dx == 0 || (posX + dx) &lt; 0 || (posX + dx) &gt;= Labyrinth.Size || MyExplorationMap[posX + dx, posY] == "1") continue; var tempPos = new int[2]; tempPos[0] = posX + dx; tempPos[1] = posY; validPaths.Add(tempPos); } for (var dy = -1; dy &lt;= 1; dy++) { if (dy == 0 || (posY + dy) &lt; 0 || (posY + dy) &gt;= Labyrinth.Size || MyExplorationMap[posX, posY + dy] == "1") continue; var tempPos = new int[2]; tempPos[0] = posX; tempPos[1] = posY + dy; validPaths.Add(tempPos); } //This method checks up, down, left, right and returns the posible routes as `int[]` for each one return validPaths; } </code></pre> <p>CheckSurroundings uses the deep copy passed to the child (via the constructor) to validate the routes he can take. It is NOT intended to alterate the parent's copy, because he is now in ANOTHER path of the labyrinth. The child only needs the information updated (passed via the constructor) UNTIL the point they "separate". And also EACH child must be independent from the other childs. That's what I'm trying to do. But I'm not sure what's wrong, maybe concurrency problem? Please help. If you need anything else let me know. –</p>