Click here to Skip to main content
15,887,965 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to show progress bar on a windows application in C# for a windows service having unknown process time?

I am using a windows application where there is a Run Button to start the service and Stop Button inorder to stop the same.

Here Process may take shorter and longer time which depends.

Below is my Code in C#

C#
#region btnRun_Click
       //Automate the service on clicking Run Button
       private void btnRun_Click(object sender, EventArgs e)
       {
           //To show service started message on button click(Status formload boolean varaiable set)
           formload = false;
           StartServiceCheck();

           

       }
       #endregion
         #region StartServiceCheck Function
       //To check whether the Archival Automation is installed or not and if installed start the service
       private void StartServiceCheck()
       {
           startservice = false;
           serviceInstallationStatus = AS.CheckServiceInstallation(windowsservice);
         //Check Archival Automation is Installed in Services.msc
           if (serviceInstallationStatus == true)
           {
               //Start the Archival Automation installed in Services.msc
               startservice= AS.StartService(windowsservice);
               if (formload == true)
               {
                   btnRun.Enabled = false;
                   return;
               }
               //Check whether the Archival Automation is running and if so Run button will be diabled else Run button will be enabled
              if (startservice == true)
              {
                  //Disable the Run Button if Service is Already Running
                    btnRun.Enabled = false;
                    //Show message box if the Archival Automation is Already running and to shown only on Run Button Click

                  MessageBox.Show("Archival Automation is Already Running");
              }
                  //If the Archival Automation is not running
              else
              {
                  //Enable the Run Button
                  btnRun.Enabled = true;
                  //Message to be shown only on Run Button Click with status formload
                  if(formload==false)
                  MessageBox.Show("Archival Automation has been started Successfully");



              }
              btnStop.Enabled = true;

           }
           //Message Box if Archival Automation is not Installed
           else
           {
               MessageBox.Show("Archival Automation is Not Installed");
           }


       }
Posted
Comments
[no name] 14-Jul-14 11:20am    
If you are manually starting your service to do some task, what is the point of having a service at all?
Sergey Alexandrovich Kryukov 14-Jul-14 11:55am    
Agree with Was Aday. And you need to tag your UI application type or the UI library you are using. It would be pointless to try to advise on all you could possibly use.
—SA

There is several types of progress bar control. Have a look here: Progress Bars[^]. The one you want to use depends on your needs. I'd suggest you to use progress bar with marquee (or animated image) ;)
 
Share this answer
 
Hi Rahul,

Progressbar has a style for indetermined processes.
myProgressBar.Style = ProgressBarStyle.Marquee;
 
Share this answer
 
Comments
Rahul 105 16-Jul-14 4:31am    
Set a progress bar with Marquee style with min and max entries.Just check whether the processes is existing in the task manager and when done the progres bar will be hidden with a thread with a sleep of 2 sec just to set some time span to hide the progress bar.
Code as follows:
private void ProgressBarStop()
{
Thread.Sleep(2000);
string process = "ArchivalService";
if (Process.GetProcessesByName(process).Length == 0)
{
ArchivalprgsbrProgress.Visible = false;
lblArchivalProgress.Visible = false;
}
else
{
ArchivalprgsbrProgress.Visible = true;
lblArchivalProgress.Visible = true;
}
}

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