Skip to main content
Tweeted twitter.com/StackCodeReview/status/1524494568212815876

I have a timerTimer:

 private async void QueryReportTimerCallback(object state)
            {
                if (await semaphoreQueryReportSlim.WaitAsync(10))
                {
                    try
                    {
                        if (machineConfigurations != null)
                        {
                            await Task.Factory.StartNew(() =>
                                Parallel.ForEach(machineConfigurations.Where(x => x.QueryReport), async (configuration) =>
                                {
                                    if (configuration.IsConnectionValid)
                                    {
                                        var queryReport = new QueryReport(configuration, ReportConfigurations, fileContainer, applicationConfiguration, logger);
                                        await QueryAReport(configuration, queryReport);
                                    }
                                })
                            );
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, e.Message);
                    }
                    finally
                    {
                        semaphoreQueryReportSlim.Release();
                    }
                }
            }

I have a timer:

 private async void QueryReportTimerCallback(object state)
            {
                if (await semaphoreQueryReportSlim.WaitAsync(10))
                {
                    try
                    {
                        if (machineConfigurations != null)
                        {
                            await Task.Factory.StartNew(() =>
                                Parallel.ForEach(machineConfigurations.Where(x => x.QueryReport), async (configuration) =>
                                {
                                    if (configuration.IsConnectionValid)
                                    {
                                        var queryReport = new QueryReport(configuration, ReportConfigurations, fileContainer, applicationConfiguration, logger);
                                        await QueryAReport(configuration, queryReport);
                                    }
                                })
                            );
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, e.Message);
                    }
                    finally
                    {
                        semaphoreQueryReportSlim.Release();
                    }
                }
            }

I have a Timer:

private async void QueryReportTimerCallback(object state)
{
    if (await semaphoreQueryReportSlim.WaitAsync(10))
    {
        try
        {
            if (machineConfigurations != null)
            {
                await Task.Factory.StartNew(() =>
                    Parallel.ForEach(machineConfigurations.Where(x => x.QueryReport), async (configuration) =>
                    {
                        if (configuration.IsConnectionValid)
                        {
                            var queryReport = new QueryReport(configuration, ReportConfigurations, fileContainer, applicationConfiguration, logger);
                            await QueryAReport(configuration, queryReport);
                        }
                    })
                );
            }
        }
        catch (Exception e)
        {
            logger.LogError(e, e.Message);
        }
        finally
        {
            semaphoreQueryReportSlim.Release();
        }
    }
}
added 23 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Async/await in Parallel.ForEach?

I have a timer:

   var QueryReportTimer = new Timer(QueryReportTimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));

ImportantThe important part here is the method QueryReportTimerCallbackQueryReportTimerCallback.

I have a few doubts here:

  • Is it ok to have an async void method? In general it is not, but how tocan I avoid this when the timer callback is delegate public delegate void TimerCallback(object state);?
  • Is it ok to have await inside Parallel.ForEach Parallel.ForEach?

Method QueryReportTimerCallbackQueryReportTimerCallback:

Async/await in Parallel.ForEach?

I have timer

   var QueryReportTimer = new Timer(QueryReportTimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));

Important part here is method QueryReportTimerCallback

I have few doubts here

  • Is it ok to have async void method? In general it is not, but how to avoid this when timer callback is delegate public delegate void TimerCallback(object state);?
  • Is it ok to have await inside Parallel.ForEach ?

Method QueryReportTimerCallback:

Async/await in Parallel.ForEach

I have a timer:

var QueryReportTimer = new Timer(QueryReportTimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));

The important part here is the method QueryReportTimerCallback.

I have a few doubts here:

  • Is it ok to have an async void method? In general it is not, but how can I avoid this when the timer callback is delegate public delegate void TimerCallback(object state);?
  • Is it ok to have await inside Parallel.ForEach?

Method QueryReportTimerCallback:

Source Link
Raskolnikov
  • 812
  • 2
  • 11
  • 25

Async/await in Parallel.ForEach?

I have timer

   var QueryReportTimer = new Timer(QueryReportTimerCallback, null, TimeSpan.Zero, TimeSpan.FromSeconds(15));

Important part here is method QueryReportTimerCallback

I have few doubts here

  • Is it ok to have async void method? In general it is not, but how to avoid this when timer callback is delegate public delegate void TimerCallback(object state);?
  • Is it ok to have await inside Parallel.ForEach ?

Method QueryReportTimerCallback:

 private async void QueryReportTimerCallback(object state)
            {
                if (await semaphoreQueryReportSlim.WaitAsync(10))
                {
                    try
                    {
                        if (machineConfigurations != null)
                        {
                            await Task.Factory.StartNew(() =>
                                Parallel.ForEach(machineConfigurations.Where(x => x.QueryReport), async (configuration) =>
                                {
                                    if (configuration.IsConnectionValid)
                                    {
                                        var queryReport = new QueryReport(configuration, ReportConfigurations, fileContainer, applicationConfiguration, logger);
                                        await QueryAReport(configuration, queryReport);
                                    }
                                })
                            );
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, e.Message);
                    }
                    finally
                    {
                        semaphoreQueryReportSlim.Release();
                    }
                }
            }