Property MaxRuntimeMs
Namespace: Belay.Attributes
Assembly: Belay.Attributes.dll
MaxRuntimeMs
Gets or sets the maximum runtime for the thread in milliseconds. If specified, the thread will be automatically terminated after this duration.
csharp
public int? MaxRuntimeMs { get; set; }
Property Value
int?
Examples
[Thread(MaxRuntimeMs = 300000)] // Run for 5 minutes
public async Task StartBenchmarkAsync()
{
await ExecuteAsync(@"
import _thread
import time
def benchmark():
start_time = time.ticks_ms()
iterations = 0
# Run until stop flag is set (by runtime limit)
while not globals().get('benchmark_stop', False):
perform_benchmark_iteration()
iterations += 1
# Check stop condition periodically
if iterations % 100 == 0:
if globals().get('benchmark_stop', False):
break
elapsed = time.ticks_diff(time.ticks_ms(), start_time)
print(f'Benchmark completed: {iterations} iterations in {elapsed}ms')
_thread.start_new_thread(benchmark, ())
");
}
Remarks
Use MaxRuntimeMs to prevent runaway threads or to implement time-bounded operations. This is particularly useful for data collection windows or temporary monitoring periods.
When the runtime limit is reached, the thread is gracefully terminated by setting a stop flag that the thread should check periodically.
Exceptions
Thrown when setting a runtime value that is less than or equal to zero.