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
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