예제 #1
0
        public virtual string Format(string text)
        {
            var formattedText = text;

            using (AutoResetEvent waitHandle = new AutoResetEvent(false))
            {
                Thread thread = new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        if (this.IsEnableTracing)
                        {
                            _StartTrace();
                        }
                        formattedText = innerFormatter.Format(formattedText);
                        waitHandle.Set();
                    }
                    catch
                    {
                        //Trace.Warn(te.Message, te);
                    }
                }));

                thread.IsBackground = true;
                thread.Name         = string.Format("{0} Thread", GetType().Name);
                thread.Start();

                bool timedOut = waitHandle.WaitOne(TimeoutInterval, false) == false;
                waitHandle.Close();

                if (timedOut)
                {
                    try
                    {
                        // Abort the regex thread.
                        thread.Abort();
                    }
                    catch
                    {
                        //Trace.Warn(ce.Message, ce);
                    }
                    var timeOutException = new TimeoutException(string.Format("Timeout waiting for formatter: [{0}].", innerFormatter.GetType().ToString()));

                    if (IsEnableTracing)
                    {
                        _TimeOut(timeOutException);
                    }

                    if (ThrowExceptionWhenTimeOut)
                    {
                        throw timeOutException;
                    }
                }
            }
            if (IsEnableTracing)
            {
                _EndTrace();
            }
            return(formattedText);
        }