wizklaus Ответов: 1

Асинхронное программирование async и await


он не возвращает никакого значения
Severity Code Description Project File Line Suppression State
Warning	CS4014	Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.


Что я уже пробовал:

 /* This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...).	*/
public async void ProcessRequests(object sender, ElapsedEventArgs args)
    {
      timeDelay2.Stop();
      string ID = DateTime.Now.ToString("yyyyMMddhhmmss");

      try
      {
        if (disableService == "N")
        {
          var Requests =  GetTransactions(ID);
          if (Requests != null)
          {
            int totalRows = Requests.Count;
            if (totalRows > 0)
            {
              logger.Info(" ID : " + ID + " | New Reprocessing Session Started... ");

              Parallel.ForEach(Requests,
                row =>
                {
                  //the error is at this point
                  ReprocessFailedTopUp(ID, row);
                }
                );
              logger.Info(" ID : " + ID + " | Transactions Processed : " + totalRows.ToString("N0"));
            }
          }
        }
      }
      catch (Exception exc)
      {
        logger.Error("ID : " + ID + " | Exception Occurred in Current Session | Exception : " + exc.Message);
      }
      timeDelay2.Start();
    }

1 Ответов

Рейтинг:
1

User 7429338

Предупреждение означает, что вы объявили свой метод асинхронным, в то время как на самом деле метод является синхронным. Вы можете сделать свой метод асинхронным примерно так:

public async void ProcessRequests(object sender, ElapsedEventArgs args)
{
  await Task.Run(() => {
    timeDelay2.Stop();
    string ID = DateTime.Now.ToString("yyyyMMddhhmmss");

    try {
      if (disableService == "N") {
        var Requests = GetTransactions(ID);
        if (Requests != null) {
          int totalRows = Requests.Count;
          if (totalRows > 0) {
            logger.Info(" ID : " + ID + " | New Reprocessing Session Started... ");

            Parallel.ForEach(Requests,
              row => {
              //the error is at this point
              ReprocessFailedTopUp(ID, row);
              }
              );
            logger.Info(" ID : " + ID + " | Transactions Processed : " + totalRows.ToString("N0"));
          }
        }
      }
    }
    catch (Exception exc) {
      logger.Error("ID : " + ID + " | Exception Occurred in Current Session | Exception : " + exc.Message);
    }
    timeDelay2.Start();
  });
}