Threading Question.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

An method is called on an object instance on a new thread. This method loops
eternally and does various things.

I want to be able to then set a property value on the same instance of the
object. Once set, the eternally looping method will pickup the property value
change and treat it accordingly.

How can one do this?

Thanx in advance.

Regards,
Rik
 
The child thread has access to the memory space of the parent thread object.
Put the value into a property in the parent object (which spawned the
thread) and have the child thread read it when it needs it. You may have to
use locking to ensure that the child thread doesn't attempt to read the
value while the parent object is writing it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
I don't think I explained myself well.
We have a scheduling service that actions jobs (synchronously) and their
steps at various times in the day. The service acts to poll for jobs to run
and action them (Method: m_colJobs.Start). Each poll made is followed by a
Thread.Sleep for 1 second.

Calling the method: "Stop" below causes the application to hang at the line:
if (!m_colJobs.Running)

A Stop request I am wanting to set a proprerty value e.g. ShutDown on
m_colJobs. Once set, I am wanting iterations within m_colJobs.Start to
continue, but to recognise the Shutdown request and wait untill the currently
running jobs have completed before exiting.

Any idea what I am doing wrong? Is there no easy way of doing this without
having to rely on the Threads SetData / GetData methods?

public void Start()
{
try
{
Stop(false);
KillBackgroundThread(); //m_objPrimaryThread
}
catch (Exception ex)
{
m_objPrimaryThread = null;
....
}

try
{
m_objPrimaryThread = new Thread(new ThreadStart(m_colJobs.Start));
m_objPrimaryThread.IsBackground = true;
m_objPrimaryThread.Name = "ScheduleRequestHandler";
m_objPrimaryThread.Start();
}
catch (Exception ex)
{
....
}
}

public void Stop(bool bForced)
{
//Could use thread interrupt - quicker and easier to implement as done
below.
if (m_colJobs == null)
return;

if (!m_colJobs.Running)
return;

m_colJobs.Stop(bForced); //Force the shutdown - untidy.

if (bForced)
while (m_colJobs.WaitingForShutdown)
{ System.Threading.Thread.SpinWait(10000); }
}
 
I'm not sure I follow how your service works. You say that the service "acts
to poll for jobs" but don't explain anything about this mechanism, other
than the rather odd fact that it sleeps instead of using a timer. In
addition, you use the plural word "jobs," but I only see one child thread
created one time. Clearly, there is more to this app than you've explained,
and the devil is in the details.

However, you do mention one thing that I can put my finger on:
Calling the method: "Stop" below causes the application to hang at the
line:
if (!m_colJobs.Running)

But I don't have any information about this property or field of the class
being referenced. How does the m_colJobs class know whether it's running or
not? Is this a property? If so, how does the getter method work?

I write a similar service, but for this service I wrote a class that handles
its own threads. The service simply calls the Start and Stop methods of the
class. The Start method tells the class to start its "Run" thread. The Stop
method tells it to stop its "Run" thread. The class notifies the service of
its state by raising various events from the child thread. The service
subscribes to the events and uses the reported state of the class, and its
various properties (set by the child thread) to determine when to exit on
stop.

But the salient point of this is that the "Run" child thread does *not*
continue indefinitely. And this is probably the crux of your problem. The
"Run" thread is a method, which means that it executes *without
interruption* until it is finished. The method raises events at various
points of its execution, as well as setting properties in its parent class,
which can be polled by the service if necessary. But the thread executes one
time and then exits. The parent class uses a timer to determine how often to
run the method, *not* an infinite loop. The timer's Elapsed event triggers
the method to run. So, there is always an opportunity to simply stop the
timer, and once the method thread has finished, it is not restarted.

This way, the service can call the "Stop" method of the class, which simply
stops the timer. The service can then wait for the "Finished" property of
the class (set by the child thread) to become true, and can then exit
gracefully.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Back
Top