Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# method name expected

Tags:

c#

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();
                }
            }
        }
    }
like image 535
Usher Avatar asked Jan 11 '12 04:01

Usher


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

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);
    }
}
like image 130
Jon Skeet Avatar answered Oct 07 '22 01:10

Jon Skeet


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;

    ...
}
like image 25
ChrisWue Avatar answered Oct 07 '22 01:10

ChrisWue