Monday, June 08, 2009

How to Detected If Current Thread Is Interrupted in .Net

When porting an application from Java to C#. The missing of Thread.IsInterrupted in .Net making me to look for some alternatives. At this moment, I'm using the code below but if anyone come up with a better idea please let me know.

        public static bool IsCurrentThreadInterrupted()
        {
            try
            {
                Thread.Sleep(0); // get exception if interrupted.
            }
            catch (ThreadInterruptedException)
            {
                return true;
            }
            return false;
        }

Update (6/9/2009): Also found interesting article about Thread.Sleep:

No comments:

Post a Comment