简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM