13

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();
                }
            }
        }
    }
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
Usher
  • 2,146
  • 9
  • 43
  • 81
  • 1
    "It's throwing an error" isn't very precise - what's the exception? (It would help if you'd log the actual exception, rather than just using a bare catch block.) Note that the code you've posted isn't even valid - you've given an `else` block with no `if` block. – Jon Skeet Jan 11 '12 at 04:11

3 Answers3

30

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);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Am getting this error even though i followed your instruction "the best overloaded method match for system.threading.thread.thread has some invalid arguments" – Usher Jan 11 '12 at 04:24
  • @Usher: You shouldn't... with the lambda expression code that should be fine, unless your method *actually* has a different signature (e.g. it returns a value). Given that the code you've given won't compile, and both methods are instance methods when they should probably be static methods, that may well be the case... giving a short but complete and *accurate* example would really help here. (I've just included a short but complete example which *does* work...) – Jon Skeet Jan 11 '12 at 04:25
  • It works fine with Lambada exps but am just wondering why this shouldn't work with Thread t_PerthOut = new Thread(ReadCentralOutQueue); t_PerthOut.Start("test"); can you please knowledge me – Usher Jan 11 '12 at 04:42
  • Thanks a ton Jon,chris and Robert for the timely help. – Usher Jan 11 '12 at 04:47
  • @Usher: That doesn't work because the signature for `ReadCentralOutQueue` doesn't match either that of `ThreadStart` or `ParameterizedThreadStart`. – Jon Skeet Jan 11 '12 at 11:12
  • Hm… But, if that works ­— does anybody needs either of «ThreadStart()» or «ParameterizedThreadStart()» at all? Are here a reason to use it when possible? – Hi-Angel Nov 18 '14 at 14:43
  • 1
    @Hi-Angel: If you're going to call the `Thread` constructor, you'll need one of those two - the lambda expression is just creating a `ThreadStart` here using an implicit conversion. – Jon Skeet Nov 18 '14 at 14:55
2

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;

    ...
}
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
  • why am getting this error "the best overloaded method match for system.threading.thread.thread has some invalid arguments" – Usher Jan 11 '12 at 04:24
1

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();
Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • is this correct ? ThreadStart starter = delegate { ReadCentralOutQueue ("myUrl"); }; new Thread(starter).Start(); – Usher Jan 11 '12 at 04:23