/** * [Cancel async tasks after a period of time (C#)](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/cancel-async-tasks-after-a-period-of-time) */ public async Task runAsync() { try { Console.WriteLine("{0} - run", this.getLogTag()); tokenSource.CancelAfter(timeout); T result = await runTaskAsync().ConfigureAwait(false); if (null != successCallback) { successCallback.Invoke(result); } } catch (Exception cause) { IeRuntimeException error = IeUtils.IeAppErrorHandler1(cause); // if (cause is TaskCanceledException) { // Console.WriteLine("{0} - Task has been cancelled!", this.getLogTag()); // error = new IeRuntimeException("Task has been cancelled!!", cause, Base.ASYNC_TASK_TIMEOUT); // } // else if (cause is IeRuntimeException) { // error = cause as IeRuntimeException; // Console.WriteLine("{0} - IeRuntimeException errorCode: {1}, message: {2}", this.getLogTag(), error.exceptionCode, error.Message); // } // else { // Console.WriteLine("{0} - Unknown error {1}", this.getLogTag(), cause.Message); // error = new IeRuntimeException("Unknown Error on runAsync()", cause, "99999"); // } if (null != errorCallback) { errorCallback.Invoke(error); } } finally { disposeTask(); } }
public static IeRuntimeException IeAppErrorHandler1(Exception cause) { if (cause is TaskCanceledException) { Console.WriteLine("{0} - Task has been cancelled!", "Utils-IeAppErrorHandler1"); return(new IeRuntimeException("Task has been cancelled!!", cause, Base.ASYNC_TASK_TIMEOUT)); } else if (cause is IeRuntimeException) { IeRuntimeException error = cause as IeRuntimeException; Console.WriteLine("{0} - IeRuntimeException errorCode: {1}, message: {2}", "Utils-IeAppErrorHandler1", error.exceptionCode, error.Message); return(error); } else { Console.WriteLine("{0} - Unknown error {1}", "Utils-IeAppErrorHandler1", cause.Message); return(new IeRuntimeException("Unknown Error on runAsync()", cause, "99999")); } }
protected override async Task runTaskAsync() { try { if (Thread.CurrentThread.Name == null) { Thread.CurrentThread.Name = "Thread Caller1"; } Console.WriteLine("TeskAsyncAwaitTask5 on {0}", Thread.CurrentThread.Name); int result = await getIntWithDelayAsync(); if (Thread.CurrentThread.Name == null) { Thread.CurrentThread.Name = "Thread Caller2"; } Console.WriteLine("TeskAsyncAwaitTask5 - result :{0} on {1}", result, Thread.CurrentThread.Name); ie.structures.IeApiResponse <int?> response = new ie.structures.IeApiResponse <int?>(result, null); if (null != callback) { callback.Invoke(response); } } catch (Exception cause) { Console.WriteLine("TeskAsyncAwaitTask5 - Error Message :{0} on {1}", cause.Message, Thread.CurrentThread.Name); IeRuntimeException error; if (cause is IeRuntimeException) { error = ((IeRuntimeException)cause); } else { error = new IeRuntimeException("TeskAsyncAwaitTask5 - Error on run(): [" + cause.Message + "]", cause, "00000"); } ie.structures.IeApiResponse <int?> response = new ie.structures.IeApiResponse <int?>(null, error); if (null != callback) { callback.Invoke(response); } } finally { tokenSource.Dispose(); } }
public IeApiResponse(T result, IeRuntimeException error) { this.result = result; this.error = error; }