Skip to content

tom1978-coder/TimerDemo

Repository files navigation

TimerDemo (Blazor WebAssembly, .NET 8)

A Blazor WebAssembly demo showcasing a lightweight hierarchical task timing & progress reporting utility. It simulates “making breakfast” by orchestrating parallel and nested tasks (coffee, toast, eggs, bacon, juice) while demonstrating:

  • Nested task timing (parent / child relationships)
  • Weighted progress aggregation (percent of total work)
  • Start / complete / cancel / error / expiration events
  • Max duration vs expiration semantics
  • Cooperative cancellation via CancellationTokenSource
  • Parallel slice processing (toast & bacon) using Parallel.ForEachAsync
  • UI status reporting via a TaskReport component

Contents

Page Route Purpose
Breakfast.razor / Full orchestration demo with cancellation + nested timers
BasicTimer.razor /timer Minimal example with three weighted tasks

Core Concepts

TaskTimer

Represents a unit of work (primary or sub-task). Features:

  • Start(), Complete(), Cancel(), Stop(), Restart()
  • Optional: PercentOfWorkForParentTask
  • MaxDurationInSeconds → triggers "Error" (burnt)
  • ExpirationInSeconds → triggers "Expired" (cold/stale)
  • ParentTask + automatic aggregation of progress
  • Event hooks (e.g., OnCancelEvent)
  • Elapsed time tracking (Stopwatch + timers)

TaskReport (UI Facade)

(Not shown here, but inferred)

  • Registers TaskTimer instances
  • Emits status lines (Started / Completed / Error / Expired / Cancelled / Info / Alert)
  • Provides SimulateDelay(...) helper (wrapping Task.Delay + cancellation)

Breakfast Workflow

Primary task: “Breakfast” Subtasks executed concurrently:

  • MakeCoffee (with max + expiration timers)
  • ToastBread (per-slice parallelization + optional butter/jam subtasks)
  • MakeEggs (pan warm + fry eggs, includes max + expiration)
  • FryBacon (per-slice timers with burn/cold thresholds)
  • PourJuice (simple timed task)

Cancellation triggers cleanup messages (e.g., turning off pan / coffee pot).

Basic Timer Page

Simplifies usage to three sequential weighted tasks (20% / 50% / 30%) to illustrate progress composition.

Example Usage Pattern

## Cancellation

Each sub-task can attach OnCancelEvent to perform contextual cleanup messaging.

Parallelism Notes (WebAssembly)

Parallel.ForEachAsync is used for toast & bacon slices. In WebAssembly:

  • Without multithreading enabled, this may run effectively sequentially.
  • To enable threading, ensure:
    • Property true in the project file
    • Hosted in a secure cross-origin-isolated context (COOP/COEP headers) If threading is not enabled, logic still functions—just without true parallel speedup.

Build

Prerequisites:

  • .NET 8 SDK
  • Modern browser (Chromium, Firefox, Edge)

Extending

Goal Approach
Add new task type RegisterSubTask + Start/Complete
Add retry logic Wrap SimulateDelay / work block with Polly or manual loop
Persist history Inject a state service; cache TaskEventArgs sequence
UI customization Modify TaskReport rendering (e.g., color-code statuses)
Real workloads Replace SimulateDelay with I/O or compute operations

Error vs Expiration

  • MaxDurationInSeconds → Task ran too long (treated as failure condition / “burnt”)
  • ExpirationInSeconds → Task completed (or not) but exceeded freshness window (“cold”)

Summary

TimerDemo illustrates structured, cancellable, hierarchical task timing inside a Blazor WebAssembly UI, providing an educational pattern for progress reporting, task composition, and lifecycle signaling.

Happy timing!

About

Let's take Microsoft's tutorial for async to the next level!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published