Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a child thread notify a parent thread of its status/progress?

Tags:

c#

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

have various phases to them which I need to report back.

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

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

to the calling application.

For example - I was looking to do something like this:

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;
    }

};

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

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

should reflect the current status of the job running by threadJob.

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

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?)...

like image 570
Shaitan00 Avatar asked Sep 04 '09 20:09

Shaitan00


People also ask

Do threads have parent/child relationship?

Spawning a thread Note that there is no parent/child relationship between a thread that spawns a new thread and the thread being spawned.

Does child thread see the value of parent thread local?

By default, child thread value is exactly the same as parent thread value.

Can parent thread create child threads?

Thread CreationThe creating thread is the parent thread, and the created thread is a child thread. Note that any thread, including the main program which is run as a thread when it starts, can create child threads at any time.

Which method when called from the parent thread makes parent thread wait till child thread terminates?

In java Thread join method is used, so that main or parent thread can wait for its child thread to finish its execution and die.


1 Answers

You may have already looked into this, but the BackgroundWorker class gives you a nice interface for running tasks on background threads, and provides events to hook into for notifications that progress has changed.

like image 152
Guy Starbuck Avatar answered Oct 30 '22 09:10

Guy Starbuck