Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] C# BackgroundWorker Tutorial

Thursday 25 February 2016
|
Read: Completed in minutes

[Tips] C# BackgroundWorker Tutorial

1. What's Background Worker?


Background worker same as the Thread, you can see it on Toolbox C# 4.0 (Microsoft studio 2012 above). This control can help you

C# BackgroundWorker Tutorial - Webzone Tech Tips - Zidane


- Makes threads easy to implement in Windows Forms.
- Intensive tasks need to be done on another thread so the UI does not freeze.
- It is necessary to post messages and update the user interface when the task is done.
 




2. How to use BackgroundWorker?

Background worker have 2 key properties:
 - WorkerReportsProgress
 - WorkerSupportCancellation

Background worker have 3 key functions:
 - DoWord: working
 - ProgressChanged
 - RunWorkerCompleted:When completed

C# BackgroundWorker Tutorial - Webzone Tech Tips - Zidane

GUI:

C# BackgroundWorker Tutorial - Webzone Tech Tips - Zidane

C# BackgroundWorker Tutorial - Webzone Tech Tips - Zidane

C# BackgroundWorker Tutorial - Webzone Tech Tips - Zidane
You can download full projects from here


Source code:

/*************************************************
         * Developer: ViLH (Zidane) - huuvi168@gmail.com
         * Lat Modified: 2015-02-25
         * Demo run 100% done and open New form
         * 
         * [Questions]
         * Form doing task and gif image not work. Because Gif image running main Thread 
         * and task form running main Thread too. So the gif image not word.
         * 
         * [Answer]
         * This tutorial will be help you split two Thread: 
         * one is for main form another is for gif image.
         * 
         * ***********************************************/
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Text;
        using System.Windows.Forms;
        
        namespace BackGroundWorker
        {
            public partial class Form1 : Form
            {
                private delegate void DelegateProgressbar();
        
                public Form1()
                {
                    InitializeComponent();
                    backgroundWorker1.WorkerReportsProgress = true;
                    backgroundWorker1.WorkerSupportsCancellation = true;
                }
        
                private void backgroundWorker1_RunWorkerCompleted(object sender, 
                                                  RunWorkerCompletedEventArgs e)
                {
                    if (e.Cancelled == true)
                    {
                        resultLabel.Text = "Canceled!";
                    }
                    else if (e.Error != null)
                    {
                        resultLabel.Text = "Error: " + e.Error.Message;
                    }
                    else
                    {
                        resultLabel.Text = "Done!";
        
                        // do something when finished
                        openForm();
                        this.Hide();
                    }
                }
        
                // Progress Changed
                private void backgroundWorker1_ProgressChanged(object sender, 
                                                   ProgressChangedEventArgs e)
                {
                    resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
                }
        
                // Start Thread
                private void startAsyncButton_Click(object sender, EventArgs e)
                {
                    if (backgroundWorker1.IsBusy != true)
                        backgroundWorker1.RunWorkerAsync();
                }
        
                // Cancel Thread
                private void cancelAsyncButton_Click(object sender, EventArgs e)
                {
                    if (backgroundWorker1.WorkerSupportsCancellation == true)
                        backgroundWorker1.CancelAsync();
        
                }
                
                private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
                {
                    BackgroundWorker worker = sender as BackgroundWorker;
        
                    for (int i = 1; i <= 10; i++)
                    {
                        if (worker.CancellationPending == true)
                        {
                            e.Cancel = true;
                            break;
                        }
                        else
                        {
                            // Perform a time consuming operation and report progress.
                            System.Threading.Thread.Sleep(500);
                            worker.ReportProgress(i * 10);
                        }
                    }           
                }
        
                // Because of BackgroundWorker is a Thread, so we should use 
                // InvokeRequired for open form
                private void openForm()
                {
                    if (InvokeRequired)
                        this.Invoke(new DelegateProgressbar(openForm), null);
                    else
                    {
                        Form2 frm = new Form2();
                        frm.Show();
                    }
                }
            }
        }
        


You can download full projects from here

You can view MSDN from here

Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about C# BackgroundWorker Tutorial , please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋  Webzone Tech Tips, all things Tech Tips for web development  - I am Zidane, See you next time soon ✋✋✋✋

🙇🏼 We Appreciate Your Comments and Suggestions - Webzone - all things Tech Tips web development 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com