<p>I have some code for starting a thread on the .NET CF 2.0:</p>

<pre><code>ThreadStart tStart = new ThreadStart(MyMethod);
Thread t = new Thread(tStart);
t.Start();
</code></pre>

<p>If I call this inside a loop the items completely out of order. How do introduce a wait after <code>t.Start()</code>, so that the work on the thread completes before the code continues? Will BeginInvoke/EndInvoke be a better option for this than manually creating threads?</p>

[{'answer_id': 6935, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 2, 'selected': False, 'text': "<p>If I'm reading this properly, you're starting work items on bunch of threads but you're concerned that the threads are completing out of order, and, would prefer to wait until each thread completes in the order they were started ? </p>\n\n<p>If this is the case then I'm not sure why you're creating a thread per work item. Maybe you could expand?</p>\n\n<p>If you did need to wait for a thread to complete the you could just do:</p>\n\n<pre><code>t.Join();\n</code></pre>\n"}, {'answer_id': 7101, 'author': 'Dominic Cooney', 'author_id': 878, 'author_profile': 'https://Stackoverflow.com/users/878', 'pm_score': 5, 'selected': True, 'text': '<p>How much order do you need to impose on the threads? If you just need all of the work started in the loop to finish before the code continues, but you don\'t care about the order the work within the loop finishes, then calling Join is the answer. To add more detail to <a href="https://stackoverflow.com/questions/6890/compact-framework-how-to-wait-for-thread-complete-before-continuing#6935">Kevin Kenny\'s answer,</a> you should call Join <em>outside</em> the loop. This means you will need a collection to hold references to the threads you started:</p>\n\n<pre><code>// Start all of the threads.\nList&lt;Thread&gt; startedThreads = new List&lt;Thread&gt;();\nforeach (...) {\n  Thread thread = new Thread(new ThreadStart(MyMethod));\n  thread.Start();\n  startedThreads.Add(thread);\n}\n\n// Wait for all of the threads to finish.\nforeach (Thread thread in startedThreads) {\n  thread.Join();\n}\n</code></pre>\n\n<p>In contrast, if you called Join inside the loop, the result would basically be the same as not using threads at all. Each iteration of the loop body would create and start a thread but then immediately Join it and wait for it to finish.</p>\n\n<p>If the individual threads produce some result (write a message in a log, for example) then the messages may still appear out of order because there\'s no coordination between the threads. It is possible to get the threads to output their results in order by coordinating them with a Monitor.</p>\n'}, {'answer_id': 17858, 'author': 'ollifant', 'author_id': 2078, 'author_profile': 'https://Stackoverflow.com/users/2078', 'pm_score': 3, 'selected': False, 'text': '<p>Another way of waiting for a thread to finish is using an <code>AutoResetEvent</code>.</p>\n\n<pre><code>private readonly AutoResetEvent mWaitForThread = new AutoResetEvent(false);\n\nprivate void Blah()\n{\n    ThreadStart tStart = new ThreadStart(MyMethod);\n    Thread t = new Thread(tStart);\n    t.Start();\n\n    //... (any other things)\n    mWaitForThread.WaitOne();\n}\n\nprivate void MyMethod()\n{\n     //... (execute any other action)\n     mWaitForThread.Set();\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/6890', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/636/']