Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use/cast an Action for/to a ThreadStart?

Both are delegates and have the same signature, but I can not use Action as ThreadStart.

Why?

Action doIt;
doIt = () => MyMethod("test");
Thread t;

t = new Thread(doIt);
t.Start();

but this seems to work:

Thread t;

t = new Thread(() => MyMethod("test"));
t.Start();
like image 411
Rookian Avatar asked Mar 21 '10 20:03

Rookian


1 Answers

Because thread start is a separate delegate type and Action cannot be converted to ThreadStart.

This case works because here your lambda is treated by compiler as ThreadStart:

Thread t;

t = new Thread(() => MyMethod("test"));
t.Start();
like image 139
Andrew Bezzub Avatar answered Oct 21 '22 15:10

Andrew Bezzub