Пример #1
0
        /// <summary>
        /// Run the callback in a new thread immediately.  If the thread exits with an exception log it but do
        /// not propogate it.
        /// </summary>
        /// <param name="callback">Code for the thread to execute.</param>
        /// <param name="obj">Object to pass to the thread.</param>
        /// <param name="name">Name of the thread</param>
        public static void RunInThread(WaitCallback callback, object obj, string name, bool log = false)
        {
            if (Util.FireAndForgetMethod == FireAndForgetMethod.RegressionTest)
            {
                Culture.SetCurrentCulture();
                callback(obj);
                return;
            }

            ThreadStart ts = new ThreadStart(delegate()
            {
                try
                {
                    Culture.SetCurrentCulture();
                    callback(obj);
                }
                catch (Exception e)
                {
                    m_log.Error(string.Format("[WATCHDOG]: Exception in thread {0}.", name), e);
                }
                finally
                {
                    try
                    {
                        Watchdog.RemoveThread(log: false);
                    }
                    catch { }
                }
            });

            StartThread(ts, name, false, log: log);
        }
Пример #2
0
        private void ProcessRequests()
        {
            while (IsRunning || m_jobQueue.Count > 0)
            {
                try
                {
                    CurrentJob = m_jobQueue.Take(m_cancelSource.Token);
                }
                catch (ObjectDisposedException e)
                {
                    // If we see this whilst not running then it may be due to a race where this thread checks
                    // IsRunning after the stopping thread sets it to false and disposes of the cancellation source.
                    if (IsRunning)
                    {
                        throw e;
                    }
                    else
                    {
                        m_log.DebugFormat("[JobEngine] {0} stopping ignoring {1} jobs in queue",
                                          Name, m_jobQueue.Count);
                        break;
                    }
                }
                catch (OperationCanceledException)
                {
                    break;
                }

                if (LogLevel >= 1)
                {
                    m_log.DebugFormat("[{0}]: Processing job {1}", LoggingName, CurrentJob.Name);
                }

                try
                {
                    CurrentJob.Action();
                }
                catch (Exception e)
                {
                    m_log.Error(
                        string.Format(
                            "[{0}]: Job {1} failed, continuing.  Exception  ", LoggingName, CurrentJob.Name), e);
                }

                if (LogLevel >= 1)
                {
                    m_log.DebugFormat("[{0}]: Processed job {1}", LoggingName, CurrentJob.Name);
                }

                CurrentJob = null;
            }

            Watchdog.RemoveThread(false);
            m_finishedProcessingAfterStop.Set();
        }
Пример #3
0
        public static Thread StartThread(
            ThreadStart start, string name, ThreadPriority priority, int stackSize = -1, bool suspendflow = true)
        {
            Thread thread;

            if (suspendflow)
            {
                using (ExecutionContext.SuppressFlow())
                {
                    if (stackSize > 0)
                    {
                        thread = new Thread(start, stackSize);
                    }
                    else
                    {
                        thread = new Thread(start);
                    }
                }
            }
            else
            {
                if (stackSize > 0)
                {
                    thread = new Thread(start, stackSize);
                }
                else
                {
                    thread = new Thread(start);
                }
            }

            thread.Priority     = priority;
            thread.IsBackground = true;
            thread.Name         = name;

            Watchdog.ThreadWatchdogInfo twi = new Watchdog.ThreadWatchdogInfo(thread, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS, name)
            {
                AlarmIfTimeout = false,
                AlarmMethod    = null
            };

            Watchdog.AddThread(twi, name, false);

            thread.Start();

            return(thread);
        }
        /// <summary>
        /// Start a new thread that is tracked by the watchdog
        /// </summary>
        /// <param name="start">The method that will be executed in a new thread</param>
        /// <param name="name">A name to give to the new thread</param>
        /// <param name="priority">Priority to run the thread at</param>
        /// <param name="isBackground">True to run this thread as a background
        /// thread, otherwise false</param>
        /// <param name="alarmIfTimeout">Trigger an alarm function is we have timed out</param>
        /// <param name="alarmMethod">
        /// Alarm method to call if alarmIfTimeout is true and there is a timeout.
        /// Normally, this will just return some useful debugging information.
        /// </param>
        /// <param name="timeout">Number of milliseconds to wait until we issue a warning about timeout.</param>
        /// <param name="log">If true then creation of thread is logged.</param>
        /// <returns>The newly created Thread object</returns>
        public static Thread StartThread(
            ThreadStart start, string name, ThreadPriority priority, bool isBackground,
            bool alarmIfTimeout, Func <string> alarmMethod, int timeout, bool log = true)
        {
            Thread thread = new Thread(start);

            thread.Priority     = priority;
            thread.IsBackground = isBackground;
            thread.Name         = name;

            Watchdog.ThreadWatchdogInfo twi
                = new Watchdog.ThreadWatchdogInfo(thread, timeout, name)
                {
                AlarmIfTimeout = alarmIfTimeout, AlarmMethod = alarmMethod
                };

            Watchdog.AddThread(twi, name, log: log);

            thread.Start();

            return(thread);
        }
 public static void Stop()
 {
     JobEngine.Stop();
     Watchdog.Stop();
 }