How to wait for a method to finish on another thread?
<p>I am new to multi-thread programming in C#. My problem is that I don't know how to wait for a method that is being run on another thread to finish, before it can continue to the next line. For example, something like this</p>

<pre><code>public class A
{    
    int i;

    public A()
    {
        i = 0;
    }

    protected void RunLoop()
    {
        while(i &lt; 100)
        {
            i++;
        }
    }

    public void Start()
    {
        TimerResolution.TimeBeginPeriod(1);
        runThread = new Thread(new ThreadStart(RunLoop));
        running = true;
        runThread.Start();
    }
}

public class B
{
    A classAInstance = new A();
    A.Start();
    Console.Writeline(i);
}
</code></pre>

<p>Right now, it prints 0 on the console, which is not what I want (i.e. i = 100).
What is the best way to do this? BTW, I don't have access to the <em><code>runThread</code></em> that is created in <em><code>class A</code></em> </p>

<p>Thanks.</p>

<p>EDIT:</p>

<p>It was a bit difficult to solve this problem without modifying a lot codes. Therefore, we ended up with adding a condition in the <code>public void Start()</code> with which it can decide whether to run the <code>RunLoop</code> in a separate thread or not. The condition was defined using an <code>Enum</code> field.</p>

<pre><code>public void Start()
{
    TimerResolution.TimeBeginPeriod(1);
    running = true;
    if (runningMode == RunningMode.Asynchronous)
    {
        runThread = new Thread(new ThreadStart(RunLoop));
        runThread.Start();
    }
    else
    {
        RunLoop();
    }
}
</code></pre>

<p>And</p>

<pre><code>public enum RunningMode { Asynchronous, Synchronous };
</code></pre>

<p>Thanks everyone for help.</p>