Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Threading Question.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rik Beacroft - 14 Mar 2006 17:17 GMT
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
Kevin Spencer - 14 Mar 2006 20:23 GMT
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.

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer

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

> An method is called on an object instance on a new thread. This method
> loops
[quoted text clipped - 11 lines]
> Regards,
> Rik
Rik Beacroft - 15 Mar 2006 11:08 GMT
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); }
}

> 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
[quoted text clipped - 17 lines]
> > Regards,
> > Rik
Kevin Spencer - 15 Mar 2006 15:11 GMT
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.

Signature

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
[quoted text clipped - 83 lines]
>> > Regards,
>> > Rik

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.