Using Action<Action> as a param, why isn't it possible here? What can I do solve this issue?
Using Action as a param, why isn't it possible here? What can I do solve this issue?
static void Main(string[] args)
{
while (true)
{
int eventsNum = 1000;
var evt = new CountdownEvent(eventsNum);
Stopwatch sw = new Stopwatch();
Action<Action> rawThreadTest = o =>
{
new 开发者_开发百科Thread(() =>
{
o();
}).Start();
};
Action<Action> threadPoolTest = o =>
{
new Thread(() =>
{
o();
}).Start();
};
//Here I get ct error "The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly."
CallTestMethod(eventsNum, "raw thread", evt, sw, rawThreadTest);
CallTestMethod(eventsNum, "thread pool", evt, sw, threadPoolTest);
Console.ReadKey();
}
}
private static void CallTestMethod<T>(int eventsNum, string threadType, CountdownEvent evt, Stopwatch sw, Action<Action> proc)
{
sw.Restart();
evt.Reset();
for (int i = 0; i < eventsNum; i++)
{
proc(() =>
{
Thread.Sleep(100);
evt.Signal();
});
}
evt.Wait();
sw.Stop();
Console.WriteLine("Total for a {0} : {1}", threadType, sw.Elapsed);
Console.WriteLine("Avg for a {0} : {1}", threadType, sw.ElapsedMilliseconds / eventsNum);
}
}
CallTestMethod has a generic parameter T which you are not supplying a type argument for when you call it.
However, it does not seem to be getting used anyway.
You're going to feel foolish, but your CallTestMethod
method signature has an unnecessary generic parameter - <T>
.
Take it out and you're good to go.
精彩评论