фронтенд для отмены задач
This commit is contained in:
+14
-14
@@ -69,19 +69,19 @@ class Program
|
|||||||
var data = page.Value.GetData();
|
var data = page.Value.GetData();
|
||||||
Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data));
|
Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data));
|
||||||
|
|
||||||
var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest;
|
// var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest;
|
||||||
|
//
|
||||||
const int iterations = 10000;
|
// const int iterations = 10000;
|
||||||
var timer = Stopwatch.StartNew();
|
// var timer = Stopwatch.StartNew();
|
||||||
var x = 0;
|
// var x = 0;
|
||||||
for (int i = 0; i < iterations; i++)
|
// for (int i = 0; i < iterations; i++)
|
||||||
{
|
// {
|
||||||
x = loadSingleton.Next(x);
|
// x = loadSingleton.Next(x);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
timer.Stop();
|
// timer.Stop();
|
||||||
Console.WriteLine("X is {0}", x);
|
// Console.WriteLine("X is {0}", x);
|
||||||
Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds);
|
// Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds);
|
||||||
Console.WriteLine($"Time per call: {timer.Elapsed.TotalMilliseconds / iterations} ms");
|
// Console.WriteLine($"Time per call: {timer.Elapsed.TotalMilliseconds / iterations} ms");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,6 @@ namespace mROA.Implementation
|
|||||||
|
|
||||||
public enum MessageType
|
public enum MessageType
|
||||||
{
|
{
|
||||||
Unknown, FinishedCommandExecution, ExceptionCommandExecution, AsyncCancelCommandExecution, CallRequest, IdAssigning
|
Unknown, FinishedCommandExecution, ExceptionCommandExecution, AsyncCancelCommandExecution, CallRequest, IdAssigning, CancelRequest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +21,8 @@ namespace mROA.Implementation
|
|||||||
public int Id => _id;
|
public int Id => _id;
|
||||||
public int OwnerId => _representationModule.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
|
var request = new DefaultCallRequest
|
||||||
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
|
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
|
||||||
@@ -37,30 +38,54 @@ namespace mROA.Implementation
|
|||||||
_representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
|
_representationModule.GetMessageAsync<ExceptionCommandExecution>(requestId: request.Id,
|
||||||
MessageType.ExceptionCommandExecution, localTokenSource.Token);
|
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)
|
if (successResponse.IsCompletedSuccessfully)
|
||||||
{
|
{
|
||||||
|
localTokenSource.Cancel();
|
||||||
return successResponse.Result.Result!;
|
return successResponse.Result.Result!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
localTokenSource.Cancel();
|
||||||
throw errorResponse.Result.GetException();
|
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
|
var request = new DefaultCallRequest
|
||||||
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
|
{ CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() };
|
||||||
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
|
await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request);
|
||||||
|
|
||||||
var successResponse =
|
var localTokenSource = new CancellationTokenSource();
|
||||||
_representationModule.GetMessageAsync<FinalCommandExecution>(
|
|
||||||
messageType: MessageType.FinishedCommandExecution, requestId: request.Id);
|
|
||||||
var errorResponse =
|
|
||||||
_representationModule.GetMessageAsync<ExceptionCommandExecution>(
|
|
||||||
messageType: MessageType.ExceptionCommandExecution, requestId: request.Id);
|
|
||||||
|
|
||||||
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)
|
if (successResponse.IsCompletedSuccessfully)
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user