0

I have a Usercontrol which is used to Restart the installed services of the application. I am using Background worker to do this task.

I am using ServiceController to restart the services. In the DO_WORK , i've written following code..

 serviceController.Refresh();

                if (serviceController.Status == ServiceControllerStatus.Running)
                {
                    serviceController.Stop();
                    serviceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMinutes(1));
                }

                serviceController.Start();
                serviceController.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMinutes(1));

Now if i close the window in which this user control has been placed then the serviceController will get disposed.

If i restared the service and if it is still in starting phase and if the user closes the window, then serviceController will get disposed and do_work will throw an error.

What should be the best practice in this senario?

UPDATE: can i do this?

        public void Dispose(bool disposing)
        {
            if (!this.isDisposed)
            {
                if (disposing)
                {
                    if (this.Worker != null)
                    {
                        this.Worker.DoWork -= this.Worker_DoWork;
                        this.Worker.RunWorkerCompleted -= this.Worker_RunWorkerCompleted;
                    }
                }
            }
         }
Learner
  • 1,490
  • 2
  • 22
  • 35

0 Answers0