Grigory Bushuev
Grigory Bushuev

Reputation: 883

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);
    }

}

Upvotes: 0

Views: 88

Answers (2)

Enigmativity
Enigmativity

Reputation: 117057

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.

Upvotes: 2

Andrew Barber
Andrew Barber

Reputation: 40150

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.

Upvotes: 3

Related Questions