How can a child thread notify a parent thread of its status/progress?
<p>I have a service responsible for many tasks, one of which is to launch jobs (one at a time) on a separate thread (threadJob child), these jobs can take a fair amount of time and </p>

<p>have various phases to them which I need to report back.</p>

<p>Ever so often a calling application requests the status from the service (GetStatus), this means that somehow the service needs to know at what point the job (child thread) is </p>

<p>at, my hope was that at some milestones the child thread could somehow inform (SetStatus) the parent thread (service) of its status and the service could return that information </p>

<p>to the calling application.</p>

<p>For example - I was looking to do something like this:</p>

<pre><code>class Service
{
private Thread threadJob;
private int JOB_STATUS;

public Service()
    {
    JOB_STATUS = "IDLE";
    }

public void RunTask()
    {
    threadJob = new Thread(new ThreadStart(PerformWork));
    threadJob.IsBackground = true;
    threadJob.Start();
    }

public void PerformWork()
    {
    SetStatus("STARTING");
    // do some work //
    SetStatus("PHASE I");
    // do some work //
    SetStatus("PHASE II");
    // do some work //
    SetStatus("PHASE III");
    // do some work //
    SetStatus("FINISHED");
    }

private void SetStatus(int status)
    {
    JOB_STATUS = status;
    }

public string GetStatus()
    {
    return JOB_STATUS;
    }

};
</code></pre>

<p>So, when a job needs to be performed RunTask() is called and this launches the thread (threadJob). This will run and perform some steps (using SetStatus to set the new status at </p>

<p>various points) and finally finish. Now, there is also function GetStatus() which should return the STATUS whenever requested (from a calling application using IPC) - this status </p>

<p>should reflect the current status of the job running by threadJob.</p>

<p>So, my problem is simple enough...
How can threadJob (or more specifically PerformWork()) return to Service the change in status in a thread-safe manner (I assume my example above of SetStatus/GetStatus is </p>

<p>unsafe)? Do I need to use events? I assume I cannot simply change JOB_STATUS directly ... Should I use a LOCK (if so on what?)...</p>
