The WaitForAll Roadshow

OK, so I took for granted some imaginative uses of WaitForAll but lacking that, here is how I am using. First, I have a nice little class called Parallel that allows me to spin together a list of tasks (actions) and then use WaitForAll, so here it is, WaitForAll's 15 minutes of fame ...

First Parallel that allows me to spin together several Action delegates to execute, well in parallel.

 

public static class Parallel
{
    public static ParallelQuery<Action> Task(Action action)
    {
        return new Action[] { action }.AsParallel();
    }

    public static ParallelQuery<Action<T>> Task<T>(Action<T> action)
    {
        return new Action<T>[] { action }.AsParallel();
    }

    public static ParallelQuery<Action> Task(this ParallelQuery<Action> actions, Action action)
    {
        var list = new List<Action>(actions);
        list.Add(action);
        return list.AsParallel();
    }

    public static ParallelQuery<Action<T>> Task<T>(this ParallelQuery<Action<T>> actions, Action<T> action)
    {
        var list = new List<Action<T>>(actions);
        list.Add(action);
        return list.AsParallel();
    }
}

 

Next, this is an example usage from an app I'm working on that just is rendering some basic computer information via WMI and performance counters. The WMI calls can be expensive given the distance and link speed of some of the computers it will be trying to communicate with. This is the actual MVC action from my controller to return the data for an individual computer.

 

public PartialViewResult Detail(string computerName)
{
    var computer = this.Computers.Get(computerName);
    var perf = Factory.GetInstance<IPerformanceCounterProvider>();

    var detail = new ComputerDetailViewModel()
        {
            Computer = computer
        };

    try
    {
        var work = Parallel
            .Task(delegate 
            {
                // Win32_ComputerSystem
                var key = computer.Name + "_Win32_ComputerSystem";
                var system = this.Cache.Get<Win32_ComputerSystem>(key);

                if (system == null)
                {
                    using (var impersonation = computer.ImpersonateElevatedIdentity())
                    {
                        system = computer.GetWmiContext().GetInstances<Win32_ComputerSystem>().Single();
                    }

                    this.Cache.Set(key, system);
                }

                detail.TotalMemory = system.TotalPhysicalMemory;
                detail.Manufacturer = system.Manufacturer;
                detail.Model = system.Model;
                detail.NumberOfProcessors = system.NumberOfProcessors;
            })
            .Task(delegate 
            {
                // Win32_OperatingSystem
                var key = computer.Name + "_Win32_OperatingSystem";
                var os = this.Cache.Get<Win32_OperatingSystem>(key);

                if (os == null)
                {
                    using (var impersonation = computer.ImpersonateElevatedIdentity())
                    {
                        os = computer.GetWmiContext().GetInstances<Win32_OperatingSystem>().Single();
                    }

                    this.Cache.Set(key, os);
                }

                detail.OperatingSystem = os.Caption;
                detail.OSVersion = os.Version;
            })
            // Performance Counters
            .Task(delegate 
            {
                using (var impersonation = computer.ImpersonateElevatedIdentity())
                {
                    detail.AvailableBytes = perf.GetSample(computer, "Memory", "Available Bytes");
                }
            })
            .Task(delegate 
            {
                using (var impersonation = computer.ImpersonateElevatedIdentity())
                {
                    detail.TotalProcessorUtilization = perf.GetValue(computer, "Processor", "% Processor Time", "_Total");
                }
            }).WithExecutionMode(ParallelExecutionMode.ForceParallelism);

        if (!work.WaitForAll(TimeSpan.FromSeconds(15), task => task()))
        {
            return PartialView("Timeout");
        }
    }
    catch (Exception ex)
    {
        this.LogException(ex);
        return PartialView("Error.ascx");
    }

    return PartialView(detail);
}

1 Comment

  • There is actually a Parallel Task library that is part of .NET 4.0; I'm switching to that :)

    http://msdn.microsoft.com/en-us/library/dd537609(v=VS.100).aspx

Comments have been disabled for this content.