Пример #1
0
        public static bool TryAbortAndFree(ref System.Threading.Thread thread, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped, int timeout = (int)Common.Extensions.TimeSpan.TimeSpanExtensions.MicrosecondsPerMillisecond)
        {
            try { AbortAndFree(ref thread, state, timeout); }
            catch { return(false); }

            return(object.ReferenceEquals(thread, null));
        }
Пример #2
0
 /// <summary>
 /// Does the thread have the state?
 /// </summary>
 public bool _hasState(System.Threading.ThreadState state)
 {
     System.Threading.Thread thread = this.Thread;
     if (null == thread)
     {
         return(false);
     }
     if ((thread.ThreadState & state) == state)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #3
0
        public static void AbortAndFree(ref System.Threading.Thread thread, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped, int timeout = (int)Common.Extensions.TimeSpan.TimeSpanExtensions.MicrosecondsPerMillisecond)
        {
            //If the worker IsAlive and has the requested state.
            if (thread != null && (thread.IsAlive && thread.ThreadState.HasFlag(state)))
            {
                //Attempt to join
                if (false == thread.Join(timeout))
                {
                    try
                    {
                        //Abort
                        thread.Abort();
                    }
                    catch (System.Threading.ThreadAbortException) { System.Threading.Thread.ResetAbort(); }
                    catch { throw; } //Cancellation not supported
                }

                //Reset the state of the thread to indicate success
                thread = null;
            }
        }
Пример #4
0
        public static Alt.Threading.ThreadState ToThreadState(System.Threading.ThreadState src)
        {
            switch (src)
            {
            case System.Threading.ThreadState.Running:
                return(Alt.Threading.ThreadState.Running);

            case System.Threading.ThreadState.StopRequested:
                return(Alt.Threading.ThreadState.StopRequested);

            case System.Threading.ThreadState.SuspendRequested:
                return(Alt.Threading.ThreadState.SuspendRequested);

            case System.Threading.ThreadState.Background:
                return(Alt.Threading.ThreadState.Background);

            case System.Threading.ThreadState.Unstarted:
                return(Alt.Threading.ThreadState.Unstarted);

            case System.Threading.ThreadState.Stopped:
                return(Alt.Threading.ThreadState.Stopped);

            case System.Threading.ThreadState.WaitSleepJoin:
                return(Alt.Threading.ThreadState.WaitSleepJoin);

            case System.Threading.ThreadState.Suspended:
                return(Alt.Threading.ThreadState.Suspended);

            case System.Threading.ThreadState.AbortRequested:
                return(Alt.Threading.ThreadState.AbortRequested);

            case System.Threading.ThreadState.Aborted:
                return(Alt.Threading.ThreadState.Aborted);
            }

            return(Alt.Threading.ThreadState.Unstarted);
        }
Пример #5
0
        public static bool TryAbortAndFree(ref System.Threading.Thread thread, System.TimeSpan timeout, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped)
        {
            try { AbortAndFree(ref thread, timeout, state); }
            catch { return(false); }

            return(object.ReferenceEquals(thread, null));
        }
Пример #6
0
        public static bool TryAbort(ref System.Threading.Thread thread, System.TimeSpan timeout, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped)
        {
            try { Abort(ref thread, timeout, state); }
            catch { return(false); }

            return(thread == null);
        }
Пример #7
0
        public static void Abort(ref System.Threading.Thread thread, System.TimeSpan timeout, System.Threading.ThreadState state = System.Threading.ThreadState.Stopped)
        {
            //If the worker IsAlive and has the requested state.
            if (thread != null && (thread.IsAlive && thread.ThreadState.HasFlag(state)))
            {
                //Attempt to join
                if (false == thread.Join(timeout))
                {
                    try
                    {
                        //Abort
                        thread.Abort();
                    }
                    catch (System.Threading.ThreadAbortException) { System.Threading.Thread.ResetAbort(); }
                    catch { throw; } //Cancellation not supported
                }

                //Reset the state of the thread to indicate success
                thread = null;
            }
        }