Action을 param으로 사용하는 이유는 무엇입니까? 이 문제를 해결하려면 어떻게해야합니까?액션 <Action>을 매개 변수로 사용하는 이유는 무엇입니까? 이 문제를 해결하려면 어떻게해야합니까?
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'의'T' 제네릭 매개 변수의 요점은 무엇입니까? 당신은 어디서나 사용/참조하는 것 같지 않습니다. – Jamiec
조치 조금 이상합니다. 작업이 아니어야합니까? 그러나 당신은 하나의 매개 변수를 전달하지 않습니다. –
Krumelur
예, 제 잘못입니다. 이전 코드에서 "기초"를 남겼습니다. –