0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
JPPINTO
  • Home
  • Blog
  • Certifications
  • About
  • Contact
  • Shop
  • Gallery
  • Current Setup
Contact

Search

June 2, 2010 / C# (C-Sharp)

C# Windows Form is Busy (Waiting Cursor and Progress Bar)

Tags: c#, progressbar, waitingcorsor
Featured image for C# Windows Form is Busy (Waiting Cursor and Progress Bar)

This article helped me a great deal in programming this little application I’m building with C#. It was written by Ali Badereddin.


RSS Feed from: http://mycodelog.com/2010/02/11/busy/

There are two very common ways of telling the user that your application is busy. One is to show a progress bar that gets updated based on the progress getting done, and another is to show the “Waiting” cursor while the application is doing work.

Waiting Cursor

To show the user the Waiting cursor while your program is busy, all you have to do is to set the current cursor to the Waiting cursor before your code runs, then set it back to an arrow after your code completes.

Cursor.Current = Cursors.WaitCursor;
// Your Code
Cursor.Current = Cursors.Default;

Progress Bar

The progress bar is a more user-friendly solution, but in most cases showing the waiting cursor does the job. Here is the simplest way to use a progress bar:

 
int totalSteps = 10;
for (int i = 1; i <= totalSteps; i++)
{
// One chunk of your code
int progress = i * 100 / totalSteps;
blocksProgressBar.Value = progress;
}
blocksProgressBar.Value = 0;

Yes, it’s that easy to implement a progress bar that gets updated based on the work done by your app. However, while progress is shown, the user can’t interact with the UI or do any other operation (the UI thread is the single thread doing the work here). To get the multi-threaded behavior, the easiest way is to use a background worker process, as shown below:

So instead of putting your code in the event handler method, you will replace it with a call to start the worker process then move the code to the worker process events.

private void doButton_Click(object sender, EventArgs e)    
{    
backgroundWorker.RunWorkerAsync();    
}

The worker process will do its work in the DoWork event. To show progress, the code needs to be split into segments and the background worker ReportProgress method needs to be called whenever a segment of code is executed.

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int totalSteps = 10;
for (int i = 1; i <= totalSteps; i++)
{
// One chunk of your code
int progress = i * 100 / totalSteps;
backgroundWorker.ReportProgress(progress);
}
}

Whenever progress changes, we need to update the value of the progress bar.

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
blocksProgressBar.Value = e.ProgressPercentage;
}

When the worker process is done (progress = 100%), we reset the progress bar.

Private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
blocksProgressBar.Value = 0;
}

Below is a Windows Form application that lets you try the concepts explained above, and also shows you how the Marquee progress bar works, which is shockingly harder than the more realistic single-threaded progress bar we’ve discussed above.

clip_image001

Download source and exe from here.

Please check the original RSS Source for any changes or updates: http://mycodelog.com/2010/02/11/busy/

Post Views: 500
<- Hyper-V Installation on Windows Server 2008
SharePoint 2010: Create Web Application ->

Categories

  • Active Directory (5)
  • AI (5)
  • Amazon Cloud Services (1)
  • AWS (3)
  • Blazor (1)
  • C# (C-Sharp) (3)
  • CI/CD Pipelines (1)
  • Cloud (1)
  • Cloudflare (2)
  • Codex (2)
  • Containers (4)
  • Deployment (2)
  • Development (6)
  • DNS (1)
  • Docker (3)
  • Email (1)
  • Family (1)
  • General (5)
  • IIS 6.0 (4)
  • IIS 7.0 (10)
  • IIS 8.0 (1)
  • Infrastructure as Code (IaC) (1)
  • Kubernetes (3)
  • Linux (11)
  • Microsoft 365 (2)
  • MySQL (1)
  • Office 2010 (1)
  • PHP (1)
  • PowerShell (11)
  • Productivity (1)
  • Security (2)
  • Servers (9)
  • SharePoint 2007 (8)
  • SharePoint 2010 (19)
  • SharePoint 2013 (2)
  • SharePoint Online (1)
  • SMTP (4)
  • SQL Server 2008 (1)
  • SQL Server 2008 R2 (1)
  • SQL Server 2012 (2)
  • SQL Server 2019 (1)
  • SSL (1)
  • Travel (1)
  • Troubleshooting (1)
  • Ubuntu (9)
  • Uncategorized (1)
  • URL Rewrite (2)
  • Visual Studio 2019 (1)
  • Visual Studio Code (1)
  • Web Server (1)
  • Windows 10 (7)
  • Windows 2003 (9)
  • Windows 2008 (18)
  • Windows 2012 (6)
  • Windows 7 (3)
  • Windows Firewall (1)
  • Windows Server (1)
  • Windows Vista (1)
  • WordPress (5)
  • WP-CLI (3)

Recent Posts

  • Fix ‘Codex Could Not Start’ in VS Code Remote SSH on Linux
  • Move WordPress Uploads to Amazon S3 with the Uploads to S3 Plugin
  • WordPress Must-Use Plugins: MU Plugins vs. Regular Plugins
  • Install Codex Desktop on Windows Server and Control It from Your Phone
  • Create an Internal Certificate Authority for Private Websites

Advertisement

Tags

agents.md ai coding agents aws bash cloudflare cloud storage codex context engineering developer workflow dev to production dns externalize blob externalize sharepoint data full installation https IIS IIS installation index server configuration installing cumulative updates linux load balance central administration microsoft 365 nginx powerpoint powershell redirect http to https s3 server setup sharepoint 2010 cumulative updates sharepoint 2010 farm build sharepoint 2010 farm configuration sharepoint 2010 farm installation sharepoint data externalization ssl storagepoint tls ubuntu ubuntu server web server windows windows server 2008 wordpress WordPress plugins wp-cli x86
© 2026 JPPinto.com. All rights reserved.