C# – Progress Bar as Thread Wrapper

email me

Wrap a function or silent install using this C# project.

The main form with a button


The progress bar


Download the full project: ProgressBar_Wrapper_1.0.0.1.zip


Form1.cs

* tested in Visual Studio 2019

// MrNetTek
// eddiejackson.net/blog
// 12/23/2019
// free for public use 
// free to claim as your own

using System;
using System.Windows.Forms;
using System.Threading;

namespace ProgressBar
{
    public partial class Form1 : Form
    {
        private bool isProcessRunning = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            this.Hide(); // hide initial form

            if (isProcessRunning)
            {
                // MessageBox.Show("Process is running...");
                return;
            }

            ProgressDialog progressBar = new ProgressDialog();            

            // Task 1 background thread
            // add your setup or install EXE in this thread
            Thread bgThread = new Thread(
                new ThreadStart(() =>
                {          
                    isProcessRunning = true;

                    // Progress Bar Control
                    // begin
                    for (int n = 0; n < 20; n++)
                    {
                        // Add your custom code here
                        Thread.Sleep(1000);
                        progressBar.UpdateProgress(n);
                    }

                    for (int n = 21; n < 100; n++) { // Add your custom code here Thread.Sleep(100); progressBar.UpdateProgress(n); } // end Thread.Sleep(500); if (progressBar.InvokeRequired) progressBar.BeginInvoke(new Action(() => progressBar.Close()));

                    isProcessRunning = false;
                                        
                }
            ));

            // Start Process
            bgThread.Start();

            // Start Progress Bar
            progressBar.ShowDialog();

            // add other post thread code here

            this.Close(); // close form            
        }
     
    }
}


ProgressDialog.cs

// MrNetTek
// eddiejackson.net/blog
// 12/23/2019
// free for public use 
// free to claim as your own

using System;
using System.Windows.Forms;

namespace ProgressBar
{
    public partial class ProgressDialog : Form
    {
        public ProgressDialog()
        {
            InitializeComponent();
        }

        public void UpdateProgress(int progress)
        {
            if (progressBar.InvokeRequired)
                progressBar.BeginInvoke(new Action(() => progressBar.Value = progress));
            else
                progressBar.Value = progress;
            
        }

        public void SetIndeterminate(bool isIndeterminate)
        {
            if (progressBar.InvokeRequired)
            {
                progressBar.BeginInvoke(new Action(() =>
                    {
                        if (isIndeterminate)
                            progressBar.Style = ProgressBarStyle.Marquee;
                        else
                            progressBar.Style = ProgressBarStyle.Blocks;
                    }
                ));
            }
            else
            {
                if (isIndeterminate)
                    progressBar.Style = ProgressBarStyle.Marquee;
                else
                    progressBar.Style = ProgressBarStyle.Blocks;
            }
        }
    }
}

 

tags: C# Threads, C# Progress Bar, MrNetTek