Reporting Real Progress in a Legacy Kentico Background Operation

Editorial illustration.

Originally published 26 June 2023; rewritten and reviewed on 15 October 2026.

Nothing erodes trust in an admin tool faster than a progress bar that marches smoothly to 100%, pauses for five seconds, and then throws an error saying nothing was saved. In older Kentico Web Forms projects, it was common to see cms:AsyncControl examples that hardcoded a fixed percentage like 50 just to show the spinner moving. That demonstrates the UI widget, but it tells the operator nothing about whether a 5,000-row import is actually making progress or stuck in a deadlock.

Choose a unit of completed work

A percentage only means something when you know both the numerator and the denominator. Before starting a batch operation—such as syncing product SKUs, regenerating thumbnails, or updating page metadata—count the work items upfront and increment the completed count only after each unit finishes.

Keep the calculation in a small helper separate from the UI control:

static int CalculatePercent(long completed, long total)
{
    if (total <= 0) return 0;
    double value = completed * 100.0 / total;
    int rounded = (int)Math.Floor(value);
    return Math.Max(0, Math.Min(100, rounded));
}

Notice Math.Floor instead of Math.Round: rounding up means 99.6% displays as 100% while work is still running. And if the total count genuinely cannot be known ahead of time—for instance, walking an external paginated API without a total-records header—show an indeterminate spinner and a processed item count (“420 items processed”) rather than inventing a fake percentage.

Adapt the display to the installed Kentico version

Legacy Kentico Portal Engine / Web Forms installations used cms:AsyncControl with AsyncWorker and LogContext to stream status updates back to the browser. Those event signatures and status properties vary across Kentico versions, so always verify against the exact API shipped with your project’s assemblies rather than copying old forum snippets verbatim.

Two practical rules apply regardless of version:

  1. Never touch HttpContext.Current or Web Forms controls inside the background thread. Once the worker leaves the request thread, HttpContext.Current is null or unsafe. Pass plain data (user ID, site ID, selected node IDs) into the worker, and write progress to a thread-safe store such as an in-memory ConcurrentDictionary keyed by job GUID or a dedicated status table.
  2. Authorise job status reads. If your UI polls an endpoint like /admin/job-status?id=..., verify that the requesting user belongs to the same site or started the job. Otherwise, any authenticated editor can probe other users’ job logs.

In newer Xperience by Kentico or standard ASP.NET Core applications, IHostedService paired with a lightweight polling endpoint (or SignalR when real-time updates matter) replaces AsyncControl cleanly without tying background work to page lifecycle events.

Define success separately from 100%

Processing the last item in a loop is not the same as finishing the job. If your batch wraps database writes in a transaction or triggers a search index rebuild at the end, keep the status in a Finishing state until the final commit and cleanup succeed.

Use explicit states—Queued, Running, Finishing, Completed, Failed, and Cancelled—so the UI reflects reality. If an IIS AppPool recycles mid-job or an exception aborts item 312 of 500, transition the job to Failed with a safe, actionable message (such as which record ID failed validation) while keeping stack traces and connection strings in server logs.

Before calling the feature done, test three edge cases: an empty batch (0 items should complete cleanly without dividing by zero), a failure on the final item after 99% progress, and a user refreshing the browser tab halfway through. A reliable progress indicator is not about making the bar look smooth—it is about telling the operator accurately what the system has finished and what still needs attention.