-2

I have class the --- core of the class skeleton is give below:-

    class Pingdom
        {
            public static string Pingdom(List<Config> configtypes)
            {
                StringBuilder response = new StringBuilder();
                bool status = false;
                foreach(var c in configtypes)
                {
                    switch(c.Type)
                    {
                        case ConfigTypes.Database:
                            {
                                status = PingdomDB(c.ConnectionType);
                            }
                            break;
                        case ConfigTypes.API:
                            {
                                status = PingdomAPI(c.Endpoint);
                            }
                            break;
                    }
                }

                if (status)
                    return "Ping";
                else
                    return "No Ping";
            }
-------------------------------------------------------
.......................................................
} 

Now, instead of the class being static I would like for it to be in such way that I can take more of an asynchronous approach in a more robust manner.

Essentially, obtain the list of configurations but process them asynchronously.

How to go about this approach?

GilliVilla
  • 4,998
  • 11
  • 55
  • 96
  • 1
    It all depends on `PingdomDB` and `PingdomAPI`, you need to show us what you are doing in those functions for us to give any advice on how to transform them to being async calls. Also right now your code only returns the status of the last item in the list that was processed, is that what you really want? – Scott Chamberlain Apr 01 '15 at 23:22

1 Answers1

-1
    class Pingdom {
        public static string PingMe(List<Config> configtypes)
        {
            bool status = true;
            List<Task> tasks2 = new List<Task>();
            foreach (Config config in configtypes)
            {
                if (config.Type == ConfigTypes.Database)
                {
                    tasks2.Add(Task.Factory.StartNew(() => { status = status && PingdomDB(config.ConnectionType); }, TaskCreationOptions.LongRunning));
                }
                else if (config.Type == ConfigTypes.API)
                {
                    tasks2.Add(Task.Factory.StartNew(() => { status = status && PingdomAPI(config.ConnectionType); }, TaskCreationOptions.LongRunning));
                }
            }
            Task.WaitAll(tasks2.ToArray(), System.Threading.Timeout.Infinite);
            if (status)
                return "Ping";
            else
                return "No Ping";
        }
    }
svick
  • 236,525
  • 50
  • 385
  • 514
  • If these are long running tasks, then it will always return "Ping". You should wait for the task to complete or return the status as the result of the Task. Since every call to PingMe creates only 1 task, you don't need a List. – shr Apr 02 '15 at 07:40
  • Sorry, just saw that multiple tasks are created. You still need to wait for them to complete to compute the return value of status. var tasks2 = new List>(); ...... // after the foreach loop await Task.WhenAll(tasks2); tasks2.ForEach(t => status = status && t.Result); return status ? "Ping" : "No Ping"; – shr Apr 02 '15 at 08:00
  • Thanks, Scott, I flagged it. And thanks, shr, I forgot to include the WaitAll when I transferred the code. Curse my ADHD :) – Francine DeGrood Taylor Apr 02 '15 at 16:55
  • 1
    This isn't doing the work asynchronously, it's doing the work in parallel, but still synchronously. – Servy Apr 02 '15 at 16:56
  • I think the language is confusing you, Servy. These tasks are being done at the same time, so our language would indicate that was "synchronous" but that's not what it means in computer terminology. Check out this link; it has a great explanation: http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean . Synchronous just means "do we wait for it to finish before continuing execution"? – Francine DeGrood Taylor Apr 03 '15 at 19:09
  • This code is not thread-safe. You can't just update `status` like this from multiple threads and expect it to work. – svick May 02 '15 at 02:55
  • 1
    @FrancineDeGroodTaylor Yes, but Servy is still correct. You still wait for it to finish (`Task.WaitAll`). Actually making it asynchronous would at some layer call out to a non-thread backed `Task` (for example async/wait or a `TaskCompletionSource`) or callback function. The code is still synchronous you are just doing synchronous actions in parallel. If you had returned `Task` instead of string and used async/await I would *maybe* call the code asynchronous, but with a `Task.WaitAll` it definitely is not. – Scott Chamberlain May 02 '15 at 03:43