← Back to Home • Next: Task — Methods →
Runs one synchronous or asynchronous operation and shows that it is working — an animated spinner, the elapsed time, and a completion (or error) message — until it finishes.
The Task control is for a single unit of background work whose progress you cannot measure: a
save, a download, an API call. You attach the work with an Action(...)/ActionAsync(...) method,
Run() displays the spinner and elapsed time, and the control returns a StateTask
carrying the elapsed time, any output the work produced, and any exception it threw.
📊 If you can measure progress as a number, use the ProgressBar control instead. To run several operations (sequentially or in parallel) with a per-task status list, use MultiTasks. For a fixed-length wait, use Timer.
| Sub-page | What you will find |
|---|---|
| Index (this page) | What it is, when to use it, a first working example, the method map |
| Methods | Every fluent method — signature, parameters, defaults, and a snippet |
| Operations | Action overloads, contexts, elapsed time, cancellation, errors |
| Styles | The TaskStyles regions and how to recolor them |
Use Task when… |
Consider instead… |
|---|---|
| You run one operation you can’t measure | — |
| You can report a numeric progress value | ProgressBar |
| You run many operations at once | MultiTasks |
| You just need to wait a fixed duration | Timer |
using PromptPlusLibrary;
using System.Threading;
var result = PromptPlus.Controls
.Task("Processing")
.Action(token => Thread.Sleep(2000)) // the work
.Run();
if (!result.IsAborted)
PromptPlus.Console.WriteLine($"Done in {result.Content.ElapsedTime}");
Task("Processing") creates the control. The first argument is the prompt; an optional second
argument is a description line.Action(...) attaches the work. The work runs when you call Run() — there is no work delegate
on Run; you set it here..Run() shows the control and blocks until the work returns (or throws, or is cancelled).ResultPrompt<StateTask>: read .Content
for the StateTask and .IsAborted for cancellation.💡 Add
ShowElapsedTime()andSpinner(...)to give the user visible feedback that something is happening.
using PromptPlusLibrary;
using System.Threading;
using System.Threading.Tasks;
var context = new Dictionary<string, object?> { ["name"] = "PromptPlus", ["count"] = 10 };
var result = PromptPlus.Controls
.Task("Computing")
.ShowElapsedTime()
.Spinner(SpinnersType.Dots)
.Context(context) // isolated input context
.Finish("Computed!", "Computation failed!") // success / error text
.ActionAsync(async (input, token) =>
{
await Task.Delay(1500, token).ConfigureAwait(false);
int count = input.TryGetValue("count", out var raw) && raw is int c ? c : 0;
return new Dictionary<string, object?> { ["result"] = count * 2 }; // output context
})
.Run();
int doubled = result.Content.GetOutput<int>("result", out bool found);
This shows the most useful pieces together: elapsed time, a spinner, an input context the work reads, an output context it returns, and finish text. See Operations for how contexts and errors flow.
Grouped by purpose. Full signatures and examples are on the Methods page.
| Purpose | Methods |
|---|---|
| Attach the work | Action, ActionAsync |
| Data in/out | Context |
| Feedback | ShowElapsedTime, Spinner, Finish, ChangeDescription, ChangeDescriptionAsync |
| Formatting | Culture |
| Styling & behavior | Styles, Options |
| Run | Run |
Task returns ResultPrompt<StateTask>.
| Member | Meaning |
|---|---|
.IsAborted |
true when the run was cancelled (token / Esc) |
.Content |
The final StateTask struct |
StateTask members| Member | Meaning |
|---|---|
ElapsedTime |
How long the work ran (TimeSpan) |
Exception |
The exception the work threw, if any (the control captures it — it does not propagate) |
OutputContext |
The output dictionary the work returned (IReadOnlyDictionary<string, object?>?) |
GetOutput<T>(key, out found) |
Typed read of an OutputContext entry |
There is no
.Statusmember, andException is nullalone is not enough to conclude success — check.IsAbortedfirst. A run cancelled via the token (forwarded to your awaits, as recommended) throwsOperationCanceledExceptioninside the handler, which is caught silently:ExceptionstaysnullbutIsAbortedistrue. See Operations → Errors.
var result = PromptPlus.Controls.Task("Save").Action(Save).Run();
PromptPlus.Console.WriteLine($"HasError={result.Content.Exception is not null}, Elapsed={result.Content.ElapsedTime}");