internal async Task StartAsync(Action <AsyncActionResult> asyncActionCallback) { try { string result = await RunStartOrStopCommandAsync("start"); AsyncActionResult actionResult = IsSuccessfull(result) ? AsyncActionResult.Success() : AsyncActionResult.Fail(new IisServerException($"Не удалось запустить сервер: {result}")); asyncActionCallback(actionResult); } catch (Exception exception) { AsyncActionResult.Fail(new IisServerException($"Запуск сервера закончился ошибкой: {exception.Message}", exception)); } }
//despatcher public void CallAsyncAction(object sender, Args args) { IAsyncResult result = AsyncActionResult.BeginInvoke( args, new AsyncCallback( delegate(IAsyncResult ar) { AsyncResult aresult = (AsyncResult)ar; AsyncActionResultDelegate <Args> caller = (AsyncActionResultDelegate <Args>)aresult.AsyncDelegate; caller.EndInvoke(aresult); } ), sender ); //wait for the action to complete result.AsyncWaitHandle.WaitOne(); }
/// <summary> /// Копирование с обработчиком исключений /// </summary> /// <param name="copyFinishedCallback"></param> /// <returns></returns> public async Task CopyAllAsync(Action <AsyncActionResult> copyFinishedCallback) { if (copyFinishedCallback == null) { throw new ArgumentNullException(paramName: nameof(copyFinishedCallback)); } try { CheckSourceDirectoryForContent(); // Сначала удалим все файлы в паке, куда копируем await _directoryHelper.RemoveAllContentAsync(_targetDirectory); await CopyAllAsync(); copyFinishedCallback(AsyncActionResult.Success()); } catch (Exception ex) { copyFinishedCallback(AsyncActionResult.Fail(ex)); } }