预期C#方法名称

Ush*_*her 7 c#

我只是试图传递一些值,但它总是抛出一个错误.有人可以纠正我在这里缺少的东西吗?

我在这里得到错误

Thread t_PerthOut = new Thread(new ThreadStart(ReadCentralOutQueue("test"));
Run Code Online (Sandbox Code Playgroud)

我想将此字符串值传递给ReadCentralOutQueue.

class Program
    {
        public void Main(string[] args)
        {
            Thread t_PerthOut = new Thread(new ThreadStart(ReadCentralOutQueue("test"));
            t_PerthOut.Start();

        }



        public void ReadCentralOutQueue(string strQueueName)
        {
            System.Messaging.MessageQueue mq;
            System.Messaging.Message mes;
            string m;
            while (true)
            {
                try
                {



                        }
                        else
                        {
                            Console.WriteLine("Waiting for " + strQueueName + " Queue.....");
                        }
                    }
                }
                catch
                {
                    m = "Exception Occured.";
                    Console.WriteLine(m);
                }
                finally
                {
                    //Console.ReadLine();
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 22

这段代码:

Thread t_PerthOut = new Thread(new ThreadStart(ReadCentralOutQueue("test"));
Run Code Online (Sandbox Code Playgroud)

试图调用ReadCentralOutQueue创造,从结果的代表.这不会起作用,因为它是一种无效方法.通常,您将使用方法组来创建委托,或使用匿名函数(如lambda表达式).在这种情况下,lambda表达式将是最简单的:

Thread t_PerthOut = new Thread(() => ReadCentralOutQueue("test"));
Run Code Online (Sandbox Code Playgroud)

你不能只用new Thread(ReadCentralOutQueue)作为ReadCentralOutQueue不为任何签名匹配ThreadStartParameterizedThreadStart.

重要的是,您要了解为什么会收到此错误,以及如何解决此错误.

编辑:只是为了证明它确实有效,这是一个简短但完整的程序:

using System;
using System.Threading;

class Program
{
    public static void Main(string[] args)
    {
        Thread thread = new Thread(() => ReadCentralOutQueue("test"));
        thread.Start();
        thread.Join();

    }

    public static void ReadCentralOutQueue(string queueName)
    {
        Console.WriteLine("I would read queue {0} here", queueName);
    }
}
Run Code Online (Sandbox Code Playgroud)