简体   繁体   中英

Simple multi-threading - combining statements to two lines

If I have:

ThreadStart starter = delegate { MessageBox.Show("Test"); };
new Thread(starter).Start();

How can I combine this into one line of code? I've tried:

new Thread(delegate { MessageBox.Show("Test"); }).Start();

But I get this error:

The call is ambiguous between the following methods or properties: 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' and 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)'

new Thread(() => MessageBox.Show("Test")).Start();

or

new Thread((ThreadStart)delegate { MessageBox.Show("Test"); }).Start();

or

new Thread(delegate() { MessageBox.Show("Test"); }).Start();

The problem is when you declared a delegate without specifying how many parameters it had, the compiler didn't know whether you meant ThreadStart(0 parameters) or ParameterizedThreadStart(1 parameter).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM