tags:

views:

57

answers:

1

I'm trying to make a Parallel Helper class (ParallelRunner) that can be used by like in the RequestService class:

public class ParallelFunction
{
    public ParallelFunction(Func<object> function)
    {
        Function = function;
    }

    public T GetResult<T>() where T : class
    {
        return (T) Data;
    }

    public Func<object> Function { get; private set; }
    public object Data { private get; set; }
}

public static class ParallelRunner
{
    public static void Run( IEnumerable<ParallelFunction> parallelFunctions)
    {
        Parallel.ForEach(parallelFunctions, f =>{f.Data = f.Function();});
    }
}


public class RequestService
{
    public void DoParallel()
    {
        var da = new RequestDataAccess();
        var pf1 = new ParallelFunction(da.GetRequests);
        var pf2 = new ParallelFunction(da.GetRequestAnswers);

        ParallelRunner.Run(new List<ParallelFunction> { pf1, pf2 });

        var requests = pf1.GetResult<List<Request>>();
        var answers = pf2.GetResult<List<RequestAnswer>>();
    }
}

what I really would love to have was a generic ParallelFunction Class like that:

public class ParallelFunction<T>  where T : class
{
    public ParallelFunction(Func<T> function)
    {
        Function = function;
    }

    public Func<T> Function { get; private set; }
    public T Data { get; set; }
}

And instead of getting the Data From the GetResult<T> that does a cast of the desired type, getting the Data from the T Data property.

The problem is here ParallelRunner.Run(new List<ParallelFunction> { pf1, pf2 }); if ParallelFunction is with the generic i'm of course not able to add tow different types into the list.

Maybe someone have a good idea how to solve that?

+1  A: 

Use a variant generic interface IParallelFunc<out T> that ParallelFunc<T> implements. Then, a IParallelFunc<int> and a IParallelFunc<string> could both be generalized to a IParallelFunc<object> and be handled by your method.

Dario
Thanks for the tip. But the problem is that I can't still add it to a List of type List<ParallelFunction<object>>.
gsharp
^this - only if on .NET 4
AZ
jup. i'm on .net 4. it's not working with List<MyClass<object>>
gsharp