Search code examples
c#progress-barlong-running-processeslong-running-task

How can I load progressbar from start to finish while I restart SQL Server Service using C#


Sorry, I am a noob for C# programming. I have search for many times to load progressbar while restart the SQL Server Service, but there are a lot of code and it was messing me up. I don't understand what was that mean. Can you all help me out, please ?

This is my code to restart the SQL Service. I need code to load progressbar while restart the service.

private void btnRestart_Click(object sender, EventArgs e) { ServiceController sv; sv = new ServiceController("MSSQLSERVER"); sv.Stop(); sv.Start(); sv.WaitForStatus(ServiceControllerStatus.Running); }


Solution

  • To be able to show a meaningful progress whatever task you are running needs to report that progress. Starting a service will not do this, so you have options:

    1. Use a progressBar with style="Marquee", i.e. an indeterminate progress bar.
    2. Guess how long it will take, and write use a timer to fake progress reports. Optionally slowing down the closer the progress is to the end.

    You will also need to consider if you want a modal progress dialog or not. A non modal progress bar is easy, just put it somewhere on your form, and make it visible (and reset the value) before restarting your service.

    A modal progress dialog will show a new window that prevent your user from doing anything else in the application while while it is displayed, but it is somewhat more difficult to use since you need to do the actual work on a background thread, and send a message to the window to close itself when you are done. The details will depend on the UI framework used.