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?