简体   繁体   中英

.NET Threading - Quick question

What should I put instead of the "SomeType" in the below function? Delegate seems to be wrong here..

 public static void StartThread(SomeType target)
 {
     ThreadStart tstart = new ThreadStart(target);
     Thread thread = new Thread(tstart);
     thread.Start();
 }

EDIT: I'm not looking for alternative ways to write this.

您应该将ThreadStart作为参数,而不是尝试在方法中对其进行初始化。

Try the System.Action type.

Here my test code:

static void Main(string[] args)
{
    StartThread(() => Console.WriteLine("Hello World!"));

    Console.ReadKey();
}

public static void StartThread(Action target)
{
    ThreadStart tstart = new ThreadStart(target);
    Thread thread = new Thread(tstart);
    thread.Start();
}

I think there will be no Sometype, as you are calling some function that will be threaded. Isn't so? Like Thread t = new Thread(new ThreadStart(function_name_here)); t.start();

and void function_name_here() { Blah blah }

FYI there is no return type but VOID.

System.Threading.ThreadStartSystem.Threading.ParameterizedThreadStart替换SomeType

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