Example #1
0
        public bool ServiceStatus(bool bStopService)
        {
            try
            {
                bool bStatus            = false;
                int  iStopAttemptCounts = 5;

                ServiceController myService = new ServiceController();
                myService.ServiceName = "BMCExchangeImportExport";
                ServiceControllerStatus svcStatus = myService.Status;

                if (bStopService)
                {
                    if (svcStatus != ServiceControllerStatus.Stopped)
                    {
                        myService.Stop();
                        while (svcStatus != ServiceControllerStatus.Stopped)
                        {
                            if (iStopAttemptCounts <= 0)
                            {
                                throw new Exception("Maximum attempts to stop service reached");
                            }
                            myService.Refresh();
                            svcStatus = myService.Status;
                            Thread.Sleep(200);
                            iStopAttemptCounts--;
                            LogManager.WriteLog(string.Format("CExportDetails->ServiceStatus: Trying to stop BMCExchangeImportExport Attempt : {0}", iStopAttemptCounts), LogManager.enumLogLevel.Debug);
                        }
                    }

                    if (svcStatus == ServiceControllerStatus.Stopped)
                    {
                        bStatus = true;
                    }
                }
                else
                {
                    delExecute dExecute = new delExecute(myService.Start);
                    if (svcStatus != ServiceControllerStatus.Running)
                    {
                        dExecute.Invoke();
                    }
                    bStatus = true;
                }

                return(bStatus);
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
                if (bStopService)
                {
                    MessageBox.ShowBox("MessageID536", BMC_Icon.Error, BMC_Button.OK);
                }
                else
                {
                    MessageBox.ShowBox("MessageID537", BMC_Icon.Error, BMC_Button.OK);
                }
                return(false);
            }
        }
        public bool ServiceStatus(bool bStopService)
        {
            try
            {
                bool bStatus = false;
                int iStopAttemptCounts = 5;

                ServiceController myService = new ServiceController();
                myService.ServiceName = "BMCExchangeImportExport";
                ServiceControllerStatus svcStatus = myService.Status;

                if (bStopService)
                {
                    if (svcStatus != ServiceControllerStatus.Stopped)
                    {
                        myService.Stop();
                        while (svcStatus != ServiceControllerStatus.Stopped )
                        {
                            if (iStopAttemptCounts <= 0)
                                throw new Exception("Maximum attempts to stop service reached");
                            myService.Refresh();
                            svcStatus = myService.Status;
                            Thread.Sleep(200);
                            iStopAttemptCounts--;
                            LogManager.WriteLog(string.Format("CExportDetails->ServiceStatus: Trying to stop BMCExchangeImportExport Attempt : {0}" , iStopAttemptCounts),LogManager.enumLogLevel.Debug);
                        }
                    }

                    if (svcStatus == ServiceControllerStatus.Stopped)
                        bStatus = true;
                }
                else
                {
                    delExecute dExecute = new delExecute(myService.Start);
                    if (svcStatus != ServiceControllerStatus.Running)
                    {
                        dExecute.Invoke();
                    }
                    bStatus = true;
                }

                return bStatus;
            }
            catch (Exception ex)
            {
                ExceptionManager.Publish(ex);
                if (bStopService)
                    MessageBox.ShowBox("MessageID536", BMC_Icon.Error, BMC_Button.OK);
                else
                    MessageBox.ShowBox("MessageID537", BMC_Icon.Error, BMC_Button.OK);
                return false;
            }
        }