Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i created a thread
C#
RMSEmailReaderWindowServiceThread = new Thread(new ThreadStart(continuouslyThreadRunMethod));
           ////start the thread
           RMSEmailReaderWindowServiceThread.Start();

i created a method
C#
public void continuouslyThreadRunMethod()
        {
            // we're going to wait 5 minutes between calls to CTASK, so 
            // set the interval to 300000 milliseconds 
            // (1000 milliseconds = 1 second, 5 * 60 * 1000 = 300000)
            int waitTime = 300000; // 5 minutes    
            // this variable tracks how many milliseconds have gone by since 
            // the last call to CTASK. Set it to zero to indicate we're 
            // starting fresh
            //int elapsed = 0;
            // because we don't want to use 100% of the CPU, we will be 
            // sleeping for 1 second between checks to see if it's time to 
            // call CTASK
            ServiceController RMSEmailReaderStatus = new ServiceController("RMSEmailReader");
            try
            {
                // check the status of the service.if service is stop mean thread have to stop
                //!(RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
                while (true)
                {

                    ClientServerConnection();

                    Thread.Sleep(waitTime);

                    if (RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
                        this.OnStop();

                }//while close
            }//try close
            //for thread exception
            catch (ThreadAbortException TAE)
            {
                // we want to eat the excetion because we don't care if the 
                // thread has aborted since we probably did it on purpose by 
                // stopping the service.
                //if exception is raised close the application
                RMSDotNetExceptionHistory RDEH_Record = new RMSDotNetExceptionHistory();
                RDEH_Record.ModuleName = "EmailReader";
                RDEH_Record.ExceptionCode = TAE.ToString();
                RDEH_Record.ExceptionDescription = TAE.ToString();
                RDEH_Record.ExceptionDateAndTime = DateTime.Now;
                RDEH_Record.AdditionalComments = "Continously Thread Run Method created an exceptions";
                InsertARecordIntoRMSDotNetExceptionHistory(RDEH_Record);
                //Environment.Exit(0);
            }//catch close
            catch (Exception ex)
            {
                RMSDotNetExceptionHistory RDEH_Record = new RMSDotNetExceptionHistory();
                RDEH_Record.ModuleName = "EmailReader";
                RDEH_Record.ExceptionCode = ex.ToString();
                RDEH_Record.ExceptionDescription = ex.ToString();
                RDEH_Record.ExceptionDateAndTime = DateTime.Now;
                RDEH_Record.AdditionalComments = "Continously Thread Run Method created an exceptions";
                InsertARecordIntoRMSDotNetExceptionHistory(RDEH_Record);
            }
        }//Continouslytheard run method close

Error:-

Failed to stop service. System.NullReferenceException: Object reference not set to an instance of an object.
at RMSEmailReader.RMSEmailReaderClass.OnStop()
at System.ServiceProcess.ServiceBase.DeferredStop()
Posted
Updated 28-Oct-20 22:56pm
v2
Comments
Jameel VM 5-Sep-13 9:17am    
in which line you got the exception?
kalisiddayya 5-Sep-13 9:47am    
when i stop the service .the error is coming

1 solution

instead of using this.OnStop() for stooping thread you just simply use break statement, which will break your while loop and your thread is finished.
if (RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
            break;
 
Share this answer
 
Comments
kalisiddayya 5-Sep-13 9:48am    
if i call the onstop method how can i stop the thread execution
Mahesh Bailwal 5-Sep-13 11:19am    
is your problem solved?
kalisiddayya 6-Sep-13 1:40am    
yes thank u
HNDandekar 29-Oct-20 4:56am    
I am too facing the same problem. service sarts, it works, but if i do stop, it gives said error.
where to use this line? in solution above?
if (RMSEmailReaderStatus.Status.Equals("ServiceControllerStatus.Stopped"))
break;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900