This delegate takes ParameterizedThreadStart takes an object but can be called wirhout parameter


T

Tony Johansson

Hi!

The delegate ParameterizedThreadStart is declared as taking one parameter of
type object and returning void like this
public delegate void ParameterizedThreadStart(Object obj)

So according to the delegate the method signature should be void
ParameterizedThreadStart(Object obj)
In this example when I use the method ThreadWorkWithParam which is based on
the delegete ParameterizedThreadStart and look like this
private static void ThreadWorkWithParam(object param)
{
.. . .
}
I call the method ThreadWorkWithParam without passing a argument I just call
is like this
t1.Start();

It seems to me that the compiler is kindly enough to let me skip passing an
argument ?
If I for example call this method like this
ThreadWorkWithParam();
I get of cource compile error.
I thought that you have to pass an argument when the delegate is specified
as taking an argument.

class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(new
ParameterizedThreadStart(ThreadWorkWithParam));
t1.Name = "ParamThread";
t1.Start();

for (int j = 0; j < 10; j++)
{
Console.WriteLine("Main thread :" + j);
Thread.Sleep(500);
}
t1.Join();
Console.WriteLine("Press any...");
Console.ReadKey();
}

private static void ThreadWorkWithParam(object param)
{
for (int i = 0; i < (int)param; i++)
{
Console.WriteLine(Thread.CurrentThread.Name + " :" + i);
Thread.Sleep(1000);
}
}
}

//Tony
 
Ad

Advertisements

P

Peter Duniho

Tony said:
Hi!

The delegate ParameterizedThreadStart is declared as taking one parameter of
type object and returning void like this
public delegate void ParameterizedThreadStart(Object obj)

So according to the delegate the method signature should be void
ParameterizedThreadStart(Object obj)
In this example when I use the method ThreadWorkWithParam which is based on
the delegete ParameterizedThreadStart and look like this
private static void ThreadWorkWithParam(object param)
{
.. . .
}
I call the method ThreadWorkWithParam without passing a argument I just call
is like this
t1.Start();

It seems to me that the compiler is kindly enough to let me skip passing an
argument ? [...]

The compiler doesn't have anything to do with it. On the statement
where you call the Start() method, the compiler has no way to know that
you used the Thread(ParameterizedThreadStart) constructor overload to
create the object, and so could not detect and report any error based on
that information. All it knows is that you've got a Thread object, and
that you're calling the parameterless Start() method overload.

It is then up to the Thread object itself to deal with the fact that you
constructed it with a delegate for the entry point that requires an
argument, but are trying to start it without passing an argument. As
the documentation says:

Note:

If this overload is used with a thread created using
a ParameterizedThreadStart delegate, null is passed
to the method executed by the thread.

Hope that helps.

Pete
 
Ad

Advertisements

A

Alberto Poblacion

Tony Johansson said:
The delegate ParameterizedThreadStart is declared as taking one parameter
of type object and returning void like this
public delegate void ParameterizedThreadStart(Object obj)
[...]
I call the method ThreadWorkWithParam without passing a argument I just
call is like this
t1.Start();

It seems to me that the compiler is kindly enough to let me skip passing
an argument ?

Yes this is documented.
The Start method has two overloads, with and without a parameter. Under
normal circumstances, you would use the overload without parameter when the
delegate is a ThreadStart, and the overload with a parameter if the delegate
is a ParameterizedThreadStart.
So, what happens if you mix them?
In the first case, when you use Start() with a ParameterizedThreadStart
(which is what you mentioned in your question), the manual says: "If this
overload is used with a thread created using a ParameterizedThreadStart
delegate, null is passed to the method executed by the thread."
In the opposite case, i.e., if you call Start(something) when the
delegate is a ThreadStart, the manual says: "[Exceptions] [...]
InvalidOperationException - This thread was created using a ThreadStart
delegate instead of a ParameterizedThreadStart delegate."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top