Esempio n. 1
0
        private static void ThreadHelperThread(object objThreadContext)
        {
            ThreadContext tc = objThreadContext as ThreadContext;

            try
            {
                Debug.Assert(tc != null);

                tc.ManagedThreadId = Thread.CurrentThread.ManagedThreadId;
                m_logger.InfoFormat("{0} Started", tc);

                ProcessEvent(eAction.Started, ThreadStarted, tc);
                tc.SetRunning(true);

                try
                {
                    tc.Method(tc);
                }
                catch (Exception excp)
                {
                    tc.Error = excp;
                    m_logger.ErrorFormat("ERROR in {0}:\r\n{1}\r\n{2}", excp, tc, excp.Message, excp.StackTrace);

                    ProcessEvent(eAction.Error, ThreadError, tc);
                }

                m_logger.InfoFormat("{0} Completed", tc);

                tc.SetRunning(false);
                ProcessEvent(eAction.Completed, ThreadCompleted, tc);
            }
            catch (Exception excp)
            {
                tc.SetRunning(false);
                m_logger.Error(ExcpHelper.FormatException(excp, "ThreadHelperThread({0}) General ERROR", objThreadContext), excp);
            }
        }
Esempio n. 2
0
        public static ThreadContext RunThread(string sThreadName, DelegateThread dt, object objParam, ThreadPriority priority = ThreadPriority.Normal)
        {
            try
            {
                ThreadContext tc = null;

                lock (m_oLocker)
                {
                    tc = m_diNameToContext.ContainsKey(sThreadName) ? m_diNameToContext[sThreadName] : null;
                }

                if (tc == null)
                {
                    tc = new ThreadContext(sThreadName, dt, objParam);

                    Thread thread = new Thread(ThreadHelperThread);
                    thread.IsBackground = true;
                    thread.Priority     = priority;
                    thread.Name         = sThreadName;
                    thread.Start(tc);
                }

                return(tc);
            }
            catch (Exception excp)
            {
                m_logger.Error(ExcpHelper.FormatException(excp, "RunThread('{0}', {1}, {2}) General ERROR", sThreadName, dt.Method, objParam), excp);
            }

            /* DK - ThreadPool is not suitable for long runnung items.
             * ThreadPool.QueueUserWorkItem(delegate(object obj)
             * {
             * }, objParam);
             */
            return(null);
        }