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 / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

events and backgroundworker thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Analizer1 - 01 Apr 2008 20:56 GMT
Hi ...
I have lets say 4 backgroundworker thread running
they all connect to sqlserver2005 does some processing

where im having problems..its on critical errors
lets say the sqlserver is down..

so tons of errors are kicked off , where as i catch them
at different places in the code, try/catch blocks

what i want to do is stop the threads (not destroy)
and tell the service to stop

any advice on how to accomplish this.....
I did create a eventhandler but seems im still stuck in several stacks for
the threads when this event is fired

thanks
Peter Duniho - 01 Apr 2008 21:34 GMT
> [...]
> what i want to do is stop the threads (not destroy)
> and tell the service to stop
>
> any advice on how to accomplish this.....

You need to make your algorithm in the threads interruptible.  What an  
appropriate way to do this depends a lot on how you've implemented the  
threads.  But a C# event has little to do with it one way or the other.

The easiest, most straightforward mechanism would be to create a volatile  
bool variable that each thread checks every now and then to see if it  
needs to stop processing.  The flag would be set by whatever  
error-handling code you have in situations when the service needs to stop.

If you need to, you can introduce a counter or synchronization object that  
would allow your main service thread to be signaled once all of the  
threads have successfully noted that it's time to stop processing, so that  
you can proceed with stopping the service.

Pete
Analizer1 - 01 Apr 2008 21:52 GMT
thanks alot peter
I see where u are going...and i do have a boolean flag...
for telling the threads to stop...

i will work that flag into the errorhandling

the threads should not know the difference , since
if i have 4 threads running the errorhandler will set the flag once , even
though the errhandler was called 4 times

on any error each thread stops anyway...but the manager i wrote, starts a
new job for existing , non busy threads...

so just setting this flag in the errorhandler should do the trick

thanks alot
much appriciated

>> [...]
>> what i want to do is stop the threads (not destroy)
[quoted text clipped - 17 lines]
>
> Pete
Analizer1 - 02 Apr 2008 15:41 GMT
I need to Stop The Service Gracefully

ive managed to stop all the threads gracefully

but im having problems gracefully, stopping the service
small example below
the code below is not the actual code but a way to see whats going on
hope it helps

//on start of a service
public partial class Myservice //inherits in designer
{
    MyThreadManager ThreadManager=null;

protected void onStart()
{
            if (ThreadManager==null)  // 1st call on start of service
            {
                    ThreadManager= new MyThreadManager()
                    if (ThreadManager.InitThreads()==true)     // read
config file, create threads 1 to many, check sql connection
                       {
                          ThreadManager.EnableTimers()  // starts kicking
off Jobs
                       }
            }
}
}

/// code in theread Manager

public Class ThreadManager
{
           private ThreadWorkers[] _aThreads   // inherits BackGroundWorker
           private Systems.Timers.Timer[] _aTimers
           private MyLogger  Log;
            public ThreadManager()
            {

            }
           public InitThreads()
            {
                 //.. read config
                  // set up logging
                  this.log = new MyLogger();
                  this.Log.CriticalEventHandelerDelegate+=CriticalEvent
// method of this class
                  //check sql connection
                  //create threads and initilize threads
                  //CreateTimers

            {

            public void RunJobType1()   // called by timer[0]
             {
                        this._aTimer[0].Enable=false;  //below kicks off
all jobs of this jobtype
                         for (int x=0:x<_aThreads.Length;x++}
                              {
                                           if (_aThreads[x].Jobtype=1 &&
_aThreads[x].isBusy==false
                                                   &&
this._StopRunning==False)
                                                {
                                                      int TmpId=
this.GetNewJobId()
                                                      if (TmpId>0)
                                                          {
                                                              this._aThread[x].JobId=TmpId;
                                                               this._aThread[x].RunWorkerAsync();
                                                           }
                                                 {
                               {

              }
             //so a job has been kicked off above and down somwhere a fatal
event occurs i have a Event in the logging Class that if connection error
query error occurs when writing logs
             //fires a CriticalFailure Event..
    public void CriticalEvent(CriticalEventargs e)
    {
         // all the threads come here on critical events
          lock (this)
          {
                if (this._StopRunning==false);    // lets all threads know
not to work
                 {
                     this._StopRunning=True;
                     this.StopWorkers()   // waits for each thread to
finish its job...the jobs dont matter if  thery error or not the worker
always complets , errhandling is done in the business logic
                                                      //thread just runs
the business logic class
                   // once im  back here...i am ready to shut down the
Service....as u see im below the service class  i have a Reference to the
service class  ._Service
                   //when i try to stop the service the executable just
hangs...
                    whats the best way to stop a service from this
point...or do i need to get back up to the service level
                  {
           }
      }
}
> thanks alot peter
> I see where u are going...and i do have a boolean flag...
[quoted text clipped - 36 lines]
>>
>> Pete

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.