ProcessMonitor

Tracks the state, progress, and completion of a long-running process. Supports timeout monitoring, cancellation, and both synchronous and asynchronous waiting.

The ProcessMonitor can be used for Init, Load, and Index. Using the ProcessMonitor allows you to run loading and indexing in parallel.

Init, Load and LoadAllFilters put the work on a background thread and return immediately; the monitor is already running by the time they return, so it is safe to poll or wait on the very next line. Index runs on the calling thread.

Pick one way to wait — they are alternatives, not steps.

Poll for progress:

csharp
var monitor = new ProcessMonitor();
monitor.TimeoutSeconds = 120;

engine.Load(fstream, monitor);

while (monitor.IsRunning)
{
    Console.WriteLine(
quot;Progress: {monitor.ProgressPercent}%"); Thread.Sleep(200); } if (!monitor.Succeeded) Console.WriteLine(
quot;Error: {monitor.ErrorMessage}");

Wait synchronously:

csharp
engine.Load(fstream, monitor);
monitor.WaitForCompletion();          // returns false on timeout

if (!monitor.Succeeded)
    Console.WriteLine(
quot;Error: {monitor.ErrorMessage}");

Wait asynchronously:

csharp
engine.Load(fstream, monitor);
await monitor.WaitForCompletionAsync();

if (!monitor.Succeeded)
    Console.WriteLine(
quot;Error: {monitor.ErrorMessage}");

One monitor per operation. Reusing an instance while it is still running throws; wait for it first. And note that WaitForCompletionAsync on a monitor that was never handed to an operation completes immediately — it reports "nothing to wait for", not "finished successfully", so always check Succeeded.

Properties

PropertyTypeDescription
TimeoutSeconds
int
Max time a process can take before terminating. Default: Timeout.Infinite
ErrorMessage
string
Details about a failure, if any. Empty string means no error
Exception
Exception
Catches and assigns any exceptions thrown during processing
IsRunning
bool
Whether the process is currently running
ProgressPercent
int
Completion percentage of the process (0–100)
Succeeded
bool
True if the process finished successfully
StartTime
DateTime
Timestamp of when the process started
DidTimeOut
bool
True if the wait timed out before the process finished
IsCancelled
bool
True for a cancelled process
IsCompleted
bool
True for a completed process

Methods

MethodDescription
Cancel()
Requests cancellation of the monitored process
WaitForCompletion()
Waits for the process to complete, timeout, or cancel
WaitForCompletionAsync()
Waits asynchronously for the process to complete, timeout, or cancel
WaitForProcessStarted(int timeoutMs)
May be used for the sync case to avoid race conditions
Dispose()
Frees resources

ThreadPriority

Allows clients to set priority on the threads executing the process.

csharp
monitor.ThreadPriority = ThreadPriority.Normal; // default

// Options:
// ThreadPriority.Lowest
// ThreadPriority.BelowNormal
// ThreadPriority.Normal
// ThreadPriority.AboveNormal
// ThreadPriority.Highest

Constructors

csharp
ProcessMonitor()

Properties

NameTypeAccessDescription
DidTimeOut
bool
get/set
Returns true if the wait timed out before the process finished.
ErrorMessage
string
get/set
Provides a short description of the first fatal failure, if any. An empty string means no fatal error occurred. For full error details including recoverable errors see RecoverableErrors and UnrecoverableErrors.
Exception
Exception
get/set
Catches and exposes any unhandled exception thrown during processing.
IsCancelled
bool
get
Returns true for a cancelled process.
IsCompleted
bool
get
Returns true once the process has completed, regardless of outcome.
IsRunning
bool
get/set
Indicates whether the process is currently running.
MaxRecoveryAttempts
int
get/set
Controls how many recoverable errors are tolerated before the operation is aborted. A recoverable error is one where the input could be sanitized and processing continued, for example a control character or trailing comma in a JSON record. When this threshold is reached a JsonException is thrown, the operation is aborted, Succeeded is set to false, and the error that caused the limit to be exceeded is added to UnrecoverableErrors. Defaults to int.MaxValue, meaning fully tolerant. Set to 0 to treat any recoverable error as fatal.
ProgressPercent
int
get/set
Returns the completion percentage of the process, from 0 to 100.
RecoverableErrors
List<ProcessErrorCount>
get/set
Recoverable errors encountered during the operation, grouped by Source and Message. Each entry contains the ProcessError describing the first occurrence and a count of how many times that same error was seen. ParseError and Exception on each ProcessError reflect the first occurrence only. Populated as the operation runs. Cleared when the monitor is reused.
StartTime
DateTime
get/set
Timestamp of when the process started.
Succeeded
bool
get/set
Returns true if the process finished successfully.
ThreadPriority
ThreadPriority
get/set
Allows clients to set the priority of threads executing the process.
TimeoutSeconds
int
get/set
Sets the maximum time in seconds a process may run before it is considered timed out. Use Timeout.Infinite to disable the limit.
UnrecoverableErrors
List<ProcessError>
get/set
Unrecoverable errors that caused the operation to abort. Each entry is a distinct fatal error. Succeeded is set to false whenever an entry is added. Also populated when the MaxRecoveryAttempts threshold is exceeded, in which case the error that triggered the limit is promoted here from RecoverableErrors. Cleared when the monitor is reused.

Methods

Cancel

Requests cancellation of the monitored process.

csharp
void Cancel()

Dispose

Disposes the monitor and frees all resources.

csharp
void Dispose()

WaitForCompletion

Blocks until the process completes or the timeout elapses. Returns false immediately if the process was cancelled.

csharp
bool WaitForCompletion()

Returns: bool

WaitForCompletionAsync

Waits asynchronously for the process to complete or timeout.

csharp
Task<bool> WaitForCompletionAsync()

Returns: Task<bool>

WaitForProcessStarted

Blocks until the process has started. May be used in the synchronous case to avoid race conditions.

csharp
void WaitForProcessStarted(int timeoutMilliseconds = -1)
  • int timeoutMilliseconds — Default: -1

MarkFinished

Marks the process as finished. Intended for the engine's own processes and for a server hosting it.

csharp
void MarkFinished()