фронтенд для отмены задач

This commit is contained in:
2025-02-21 18:59:02 +03:00
parent ca2a04b88e
commit a887b614c7
3 changed files with 51 additions and 26 deletions
+14 -14
View File
@@ -69,19 +69,19 @@ class Program
var data = page.Value.GetData();
Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data));
var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest;
const int iterations = 10000;
var timer = Stopwatch.StartNew();
var x = 0;
for (int i = 0; i < iterations; i++)
{
x = loadSingleton.Next(x);
}
timer.Stop();
Console.WriteLine("X is {0}", x);
Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds);
Console.WriteLine($"Time per call: {timer.Elapsed.TotalMilliseconds / iterations} ms");
// var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest;
//
// const int iterations = 10000;
// var timer = Stopwatch.StartNew();
// var x = 0;
// for (int i = 0; i < iterations; i++)
// {
// x = loadSingleton.Next(x);
// }
//
// timer.Stop();
// Console.WriteLine("X is {0}", x);
// Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds);
// Console.WriteLine($"Time per call: {timer.Elapsed.TotalMilliseconds / iterations} ms");
}
}
+1 -1
View File
@@ -15,6 +15,6 @@ namespace mROA.Implementation
public enum MessageType
{
Unknown, FinishedCommandExecution, ExceptionCommandExecution, AsyncCancelCommandExecution, CallRequest, IdAssigning
Unknown, FinishedCommandExecution, ExceptionCommandExecution, AsyncCancelCommandExecution, CallRequest, IdAssigning, CancelRequest
}
}
+35 -10
View File
@@ -21,7 +21,8 @@ namespace mROA.Implementation
public int Id => _id;
public int OwnerId => _representationModule.Id;
protected async Task<T> GetResultAsync<T>(int methodId, object? parameter = default)
protected async Task<T> GetResultAsync<T>(int methodId, object? parameter = default,
CancellationToken cancellationToken = default)
{
var request = new DefaultCallRequest
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
@@ -37,30 +38,54 @@ namespace mROA.Implementation
_representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
MessageType.ExceptionCommandExecution, localTokenSource.Token);
Task.WaitAny(successResponse, errorResponse);
Task.WaitAny(new Task[]
{
successResponse, errorResponse
}, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, request.Id);
cancellationToken.ThrowIfCancellationRequested();
}
if (successResponse.IsCompletedSuccessfully)
{
localTokenSource.Cancel();
return successResponse.Result.Result!;
}
localTokenSource.Cancel();
throw errorResponse.Result.GetException();
}
protected async Task CallAsync(int methodId, object? parameter = default)
protected async Task CallAsync(int methodId, object? parameter = default,
CancellationToken cancellationToken = default)
{
var request = new DefaultCallRequest
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution>(
messageType: MessageType.FinishedCommandExecution, requestId: request.Id);
var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>(
messageType: MessageType.ExceptionCommandExecution, requestId: request.Id);
var localTokenSource = new CancellationTokenSource();
Task.WaitAny(successResponse, errorResponse);
var successResponse =
_representationModule.GetMessageAsync<FinalCommandExecution>(request.Id,
MessageType.FinishedCommandExecution,
localTokenSource.Token);
var errorResponse =
_representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
MessageType.ExceptionCommandExecution, localTokenSource.Token);
Task.WaitAny(new Task[]
{
successResponse, errorResponse
}, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CancelRequest, request.Id);
cancellationToken.ThrowIfCancellationRequested();
}
if (successResponse.IsCompletedSuccessfully)
return;