结束指定线程

Danger: dead lock or mem leak 
[DllImport("kernel32.dll")]
static extern bool TerminateThread(int hThread, uint dwExitCode);

//[STAThread]
static void Main(string[] args)
{     

    try
    {
        Thread t = new Thread(() =>
            {
               //code even interop
            });
        t.IsBackground = true; //check out this line
        t.Start();
        Console.Title = ("Hit Enter to stop");
        Console.ReadLine();
        uint dwExitCode = 0;
        TerminateThread(t.ManagedThreadId, dwExitCode); //and this line
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.ReadKey();
    }

}

转载自:https://www.cnblogs.com/read-acquisitions-docs/archive/2012/03/14/2395649.html

posted @ 2022-04-02 10:09  Heath Leger  阅读(35)  评论(0编辑  收藏  举报