人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 26, 2006

在Managed代码中慎用API

Filed under: .NET

引自
ExitThread() in managed program?
http://blogs.msdn.com/yunjin/archive/2004/01/30/65386.aspx

以下代码有问题吗?
[DllImport( "Kernel32.dll")]
public static extern void ExitThread(int exitCode);

public static void Run ()
{
    …
    // calling OS’s ExitThread to exit the current thread
    ExitThread (0);
}

public static void Main ()
{
    ThreadStart threadStart = new ThreadStart(Run);
    Thread thread = new Thread(threadStart);
    thread.Start();
    …
}

需要注意的是在CLR的控制下,某些系统调用在unmanaged和managed环境下的行为并不相同,
ExitThread就是其中一个,同时,managed threads 和unmanaged threads 也有所不同.
1.当一个managed thread退出时,比如线程处理函数返回或ThreadAbortException被抛出,
CLR会作一些清理的工作(比如stack unwinding),调用API ExitThread()或TerminateThread()
会绕过析构函数的调用,或finally 代码块.

2.在某些情况下,可能有好几个managed thread 被映射到一个OS thread,调用ExitThread 可能
会终止多个 managed thread.

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/05/26/p64/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham