Connoisseurs, please tell me how to check the execution of the method and after its completion run another method?

try { bool needUpdate = await _updater.CheckUpdateAsync(); if (needUpdate) { _updater.Update(); } } catch (Exception ex) { Logger.Current.AppendException(ex); } try { bool needUpdate = await _updater2.CheckUpdateAsync(); if (needUpdate) { _updater2.Update(); } } catch (Exception ex) { Logger.Current.AppendException(ex); } 

The meaning is: If an update is required, start the procedure. But in this code I turn to two methods at once, how to implement type checking - if the method is executed, does the second method wait for the first one and only after it starts?

  • I will add - after the completion of the method, I set the status "UpdateState.Completed:" - Elizaveta
  • And what exactly does Update () do: does it wait until the end of the update synchronously, or only starts it? If this is your function, show the code. - VladD
  • _updater.Update () starts the update method from the WowUpdater class. the code is too big for a lot of gestures - Elizabeth
  • Okay, but this method is yours? Do you have control over it? - VladD
  • Well, yes, I call this method in the update class of the main form - Elizabeth

1 answer 1

Try the following approach. We need an asynchronous method that will complete with the completion of the update. It can be remade from Update :

 public async Task UpdateAsync() { if (_currentState != UpdateState.None) return; // это не совсем правильно, тут надо дождаться окончания текущего обновления _currentState = UpdateState.Started; Logger.Current.AppendText("Began update game client"); //Запускаем выполнение кода в потоке из пула потоков. await Task.Run(() => { //Уведомляем подписчиков, что обновление запущено. OnUpdateStateChanged(UpdateState.Started); //Скачиваем патч лист Logger.Current.AppendText("Download patchlist"); 

and further as it was.

Now you can write like this:

 async Task CheckAndUpdate(WowUpdater updater) { try { bool needUpdate = await updater.CheckUpdateAsync(); if (needUpdate) { await updater.UpdateAsync(); } } catch (Exception ex) { Logger.Current.AppendException(ex); } } 

And then just

 await CheckAndUpdate(_updater); await CheckAndUpdate(_updater2); 

etc.

  • s8.hostingkartinok.com/uploads/images/2016/07/… In the class I put your code, on the basis of such errors - "The name 'updateCompletion' does not exist in the current context" and "No overload for method 'Update' takes 1 arguments "If it is convenient for you, we can chat in Skype, it is very embarrassing for me to make chat here (( - Elizabeth
  • @ Elizabeth: I am on the phone, it’s still hard to type there anyway. See, you need to add an argument to Update to compile. And stretch it to the point where SetResult is called. - VladD
  • @ Elizabeth: Well, I'll write when I get to the computer. - VladD
  • @ Elizabeth: Go to the chat, I created a room: chat.stackexchange.com/rooms/43125/checkandupdate - VladD
  • Waiting for you in the chat, the problem described - Elizabeth