应为 C# 方法名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8814014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:34:32  来源:igfitidea点击:

C# method name expected

c#

提问by Usher

I just trying to pass some values but it's throwing an error all the time. Can some one correct me what I am missing here?

我只是想传递一些值,但它一直在抛出错误。有人可以纠正我在这里缺少的东西吗?

Am getting error here

在这里出错

Thread t_PerthOut = new Thread(new ThreadStart(ReadCentralOutQueue("test"));

I want to pass this string value to ReadCentralOutQueue.

我想将此字符串值传递给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();
                }
            }
        }
    }

采纳答案by Jon Skeet

This code:

这段代码:

Thread t_PerthOut = new Thread(new ThreadStart(ReadCentralOutQueue("test"));

tries to call ReadCentralOutQueueand thencreate a delegate from the result. That isn't going to work, because it's a void method. Normally you'd use a method groupto create a delegate, or an anonymous functionsuch as a lambda expression. In this case a lambda expression will be easiest:

尝试调用ReadCentralOutQueue然后根据结果​​创建一个委托。那是行不通的,因为它是一个 void 方法。通常,您会使用方法组来创建委托或匿名函数,例如 lambda 表达式。在这种情况下,lambda 表达式将是最简单的:

Thread t_PerthOut = new Thread(() => ReadCentralOutQueue("test"));

You can't just use new Thread(ReadCentralOutQueue)as the ReadCentralOutQueuedoesn't match the signature for either ThreadStartor ParameterizedThreadStart.

您不能只使用 ,new Thread(ReadCentralOutQueue)因为 与或ReadCentralOutQueue的签名不匹配。ThreadStartParameterizedThreadStart

It's important that you understand whyyou're getting this error, as well as how to fix it.

了解为什么会出现此错误以及如何修复它,这一点很重要。

EDIT: Just to prove it doeswork, here's a short but complete program:

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

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

回答by ChrisWue

You have to do it like this:

你必须这样做:

var thread = new Thread(ReadCentralOutQueue);
thread.Start("test");

Also ParameterizedThreadStartexpects a delegate which takes an objectas parameter so you need to change your signature to this:

ParameterizedThreadStart需要一个接受objectas 参数的委托,因此您需要将签名更改为:

public static void ReadCentralOutQueue(object state)
{
    var queueName = state as string;

    ...
}

回答by Robert Rouhani

Parameters are not allowed as part of the ThreadStart delegate. There are several other solutions to passing a parameter to a new thread, discussed here: http://www.yoda.arachsys.com/csharp/threads/parameters.shtml

不允许将参数作为 ThreadStart 委托的一部分。还有其他几种将参数传递给新线程的解决方案,这里讨论:http: //www.yoda.arachsys.com/csharp/threads/parameters.shtml

But the one that would probably be simplest in your case is the anonymous method:

但是在您的情况下可能最简单的是匿名方法:

ThreadStart starter = delegate { Fetch (myUrl); };
new Thread(starter).Start();