简体   繁体   中英

C# method name expected

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 .

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

This code:

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

tries to call ReadCentralOutQueue and then create a delegate from the result. That isn't going to work, because it's a void method. Normally you'd use a method group to create a delegate, or an anonymous function such as a lambda expression. In this case a lambda expression will be easiest:

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

You can't just use new Thread(ReadCentralOutQueue) as the ReadCentralOutQueue doesn't match the signature for either ThreadStart or ParameterizedThreadStart .

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

EDIT: Just to prove it does work, 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);
    }
}

You have to do it like this:

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

Also ParameterizedThreadStart expects a delegate which takes an object as parameter so you need to change your signature to this:

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

    ...
}

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

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

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

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