Worker Initialization and Configuration

The SiticoneBackgroundWorker class extends the standard BackgroundWorker to automatically enable progress reporting and simplify the reporting of custom progress messages.

Overview

SiticoneBackgroundWorker is an enhanced implementation of the BackgroundWorker component in .NET WinForms applications. It provides built-in support for progress updates and custom messages, making it easier for developers to manage background operations without additional configuration. The primary enhancement is the ability to report progress along with an accompanying message using the ProgressChangedEx event.

Key Points

Feature
Description

Inherits BackgroundWorker

Extends the standard BackgroundWorker class to provide enhanced functionality.

Automatic Progress Reporting

WorkerReportsProgress is automatically set to true, eliminating the need for manual configuration.

Custom Progress Event

The ProgressChangedEx event supports messages along with progress updates.

Custom Progress Reporting Method

The ReportProgressEx(int percentProgress, string message) method allows reporting progress with additional context.

Best Practices

Practice
Explanation

Always subscribe to ProgressChangedEx

Since this event includes additional message functionality, developers should prefer it over ProgressChanged.

Use ReportProgressEx for clarity

This method makes progress reporting more readable and self-explanatory.

Keep background work lightweight

Avoid long-running or blocking operations inside the worker to keep the UI responsive.

Common Pitfalls

Issue
Solution

Forgetting to handle ProgressChangedEx

Ensure you attach an event handler to ProgressChangedEx to receive custom progress updates.

Calling ReportProgressEx from outside DoWork

This method should only be called from within the worker thread to avoid cross-thread exceptions.

Usage Scenarios

Scenario 1: Updating a Progress Bar with Custom Messages

A developer wants to execute a long-running task in the background and update a progress bar while displaying status messages.

Implementation:

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
using SiticoneNetFrameworkUI;

public partial class MainForm : Form
{
    private SiticoneBackgroundWorker _worker;
    private ProgressBar progressBar;
    private Label statusLabel;

    public MainForm()
    {
        InitializeComponent();
        InitializeWorker();
    }

    private void InitializeWorker()
    {
        _worker = new SiticoneBackgroundWorker();
        _worker.ProgressChangedEx += Worker_ProgressChangedEx;
        _worker.DoWork += Worker_DoWork;
    }

    private void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i <= 100; i += 10)
        {
            Thread.Sleep(500); // Simulate work
            _worker.ReportProgressEx(i, $"Processing {i}% complete");
        }
    }

    private void Worker_ProgressChangedEx(object sender, SiticoneBackgroundWorker.ProgressChangedExEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
        statusLabel.Text = e.Message;
    }
}

Real-Life Usage Scenarios

Scenario
Explanation

File Processing

While scanning or processing files, display progress along with the current file name.

Data Upload

Show progress percentage along with a status message indicating the upload stage.

Database Queries

Report progress as rows are being processed.

Troubleshooting Tips

Issue
Possible Cause
Solution

ProgressChangedEx not firing

Event handler not assigned

Ensure ProgressChangedEx has an attached delegate.

UI freezing

Work done on UI thread

Ensure long operations are inside DoWork method.

Review

Aspect
Evaluation

Ease of Integration

Simple to use with minimal setup.

Feature Enhancement

Adds useful progress messages compared to standard BackgroundWorker.

Performance

Efficient for lightweight background tasks.

Summary

Feature
Benefit

Automatic progress reporting

WorkerReportsProgress = true by default, reducing setup time.

Custom progress event

ProgressChangedEx supports progress updates with messages.

Simplified reporting method

ReportProgressEx makes progress updates more intuitive.

By following best practices and using the provided examples, developers can effectively manage long-running tasks while keeping the UI responsive.

Last updated