Custom Progress Reporting Functionality

SiticoneBackgroundWorker enhances standard progress reporting by providing an extended event and method to include custom messages with progress updates.

Overview

Custom progress reporting allows developers to track background operations more effectively by providing additional context alongside progress percentages. This feature introduces the ProgressChangedEx event and the ReportProgressEx method, enabling the transmission of messages that describe the current processing state.

Key Points

Feature
Description

Enhanced Progress Event

ProgressChangedEx event allows developers to receive both progress percentage and custom messages.

Simplified Reporting Method

ReportProgressEx(int percentProgress, string message) streamlines progress reporting.

Extended Event Arguments

ProgressChangedExEventArgs class extends ProgressChangedEventArgs to include a custom message.

Best Practices

Practice
Explanation

Use ProgressChangedEx instead of ProgressChanged

Ensures you receive both progress updates and descriptive messages.

Keep messages concise

Helps maintain clarity in progress updates without excessive verbosity.

Call ReportProgressEx from DoWork

Prevents cross-thread issues and ensures smooth progress updates.

Common Pitfalls

Issue
Solution

Using ReportProgressEx outside DoWork

Always invoke this method from within the worker thread to avoid threading issues.

Not handling ProgressChangedEx

Ensure an event handler is assigned to process progress updates effectively.

Excessive logging in ProgressChangedEx handler

Keep UI updates lightweight to prevent performance bottlenecks.

Usage Scenarios

Scenario 1: Displaying Detailed Progress Updates in a UI

A developer wants to display a progress bar while showing a textual description of the current processing state.

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 processing
            _worker.ReportProgressEx(i, $"Processing step {i}% completed");
        }
    }

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

Real-Life Usage Scenarios

Scenario
Explanation

File Copy Operations

Report progress percentage along with the current file being copied.

Data Migration

Show completion percentage while indicating the current database table being processed.

Background Calculations

Provide updates about the computation stage and estimated time remaining.

Troubleshooting Tips

Issue
Possible Cause
Solution

No progress updates received

ProgressChangedEx event not attached

Ensure the event handler is correctly assigned.

UI not updating

UI thread blocked

Perform UI updates inside ProgressChangedEx event, not DoWork.

Performance issues

Excessive calls to ReportProgressEx

Throttle updates to avoid overwhelming the UI.

Review

Aspect
Evaluation

Ease of Use

Simplifies progress reporting while adding custom messages.

Flexibility

Allows developers to provide informative progress updates.

Performance

Efficient when used correctly without excessive updates.

Summary

Feature
Benefit

ProgressChangedEx event

Allows progress tracking with messages for better UI feedback.

ReportProgressEx method

Simplifies reporting of progress updates in background tasks.

Extended event arguments

Provides additional context for progress updates.

By integrating these features properly, developers can create more informative and responsive WinForms applications.

Last updated