开发者

.NET Threading - Quick question

开发者 https://www.devze.com 2023-01-17 04:37 出处:网络
What should I put instead of the \"SomeType\" in the below function? Del开发者_JAVA技巧egate seems to be wrong here..

What should I put instead of the "SomeType" in the below function? Del开发者_JAVA技巧egate 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.


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();
}


You should have the ThreadStart as the argument instead of trying to initialize it within the method.


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.


Replace SomeType with System.Threading.ThreadStart or System.Threading.ParameterizedThreadStart.

0

精彩评论

暂无评论...
验证码 换一张
取 消