Ejemplo n.º 1
0
        private void OnTimerCallback(object state)
        {
            //if (ChoGuard.IsDisposed(this))
            //    return;

            Pause();
            try
            {
                if (_timerServiceCallback != null)
                {
                    _timerServiceCallback.Run((T)state, _timeout);
                }
                //new Action<T>(OnTimerServiceCallback).WaitFor((T)state, _timeout);
            }
            catch (TimeoutException ex)
            {
                if (!_silent)
                {
                    throw new ChoTimerServiceException(String.Format("{1}: Timeout [{0} ms] elapsed prior to completion of the method.", _timeout, _name), ex);
                }
                else
                {
                    ChoProfile.GetContext(Name).AppendIf(ChoTrace.ChoSwitch.TraceError, String.Format("Timeout [{0} ms] elapsed prior to completion of the method.", _timeout));
                }
            }
            //if (ChoGuard.IsDisposed(this))
            //    return;
            Continue();
        }
Ejemplo n.º 2
0
        public static string GetTempFileName()
        {
            try
            {
                return(Path.GetTempFileName());
            }
            catch
            {
                //Path.GetTempFileName() funtion has limitation of creating temp files. At one point,
                //will throw "File Exists" exception when it reaches the limit.
                //To avoid this error, periotically clean the temp directory before calling this function
                using (ChoAppSyncMethodExecuter syncMethodInvoke = new ChoAppSyncMethodExecuter(
                           delegate(object state)
                {
                    try
                    {
                        using (ChoProfile profile = new ChoProfile(String.Format("Temp directory [{0}] is full. Cleaning temp files...", Path.GetTempPath())))
                            ChoFile.Delete(Path.GetTempPath(), "tmp*.tmp");
                    }
                    catch { }

                    return(null);
                }
                           ))
                {
                }

                return(Path.GetTempFileName());
            }
        }
Ejemplo n.º 3
0
        public void Initialize(bool beforeFieldInit)
        {
            if (_initialized)
            {
                return;
            }
            _initialized = true;

            ChoProfile.WriteLine("Missing Name");
            if (String.IsNullOrEmpty(Name))
            {
                ChoProfile.WriteLine("Missing Name");
                return;
            }

            using (ChoBufferProfile profile = ChoBufferProfile.DelayedAutoStart(new ChoBufferProfile(true, Name, "Loading property handler...")))
            {
                try
                {
                    _propertyReplaceHandler = new ChoCallbackObj(Type, Method).CreateDelegate <ChoPropertyReplaceHandler>() as ChoPropertyReplaceHandler;
                }
                catch (Exception ex)
                {
                    profile.AppendLine(String.Format("{0}: {1}", Name, ex.ToString()));
                }
            }
        }
        /// <summary>
        /// Removes a watcher for the configuration source.
        /// </summary>
        /// <param name="configSource">
        /// The source to remove the watcher.
        /// </param>
        public static void RemoveWatcherForConfigSource(IChoConfigurationChangeWatcher configSourceWatcher)
        {
            if (_configSourceWatcherMapping.Contains(configSourceWatcher))
            {
                _configSourceWatcherMapping.Remove(configSourceWatcher);
                configSourceWatcher.Dispose();

                ChoProfile.WriteLine(String.Format("Removing Watcher: [{0}]", configSourceWatcher.EventData.ToString()));
            }
        }
        /// <summary>
        /// Sets a watcher for a configuration source.
        /// </summary>
        /// <param name="configSource">
        /// The configuration source to watch.
        /// </param>
        public static void SetWatcherForConfigSource(IChoConfigurationChangeWatcher configSourceWatcher)
        {
            if (!IsWatchingConfigSource(configSourceWatcher))
            {
                ////configSourceWatcher.ConfigurationChanged += OnConfigurationChanged;

                _configSourceWatcherMapping.Add(configSourceWatcher);

                ChoProfile.WriteLine(String.Format("Setting Watcher: [{0}]", configSourceWatcher.EventData.ToString()));
            }
        }
Ejemplo n.º 6
0
        private static void iniDocument_IgnoredEntry(object sender, ChoIniDocumentEventArgs e)
        {
            if (e == null || e.IniDocumentStates == null)
            {
                return;
            }

            foreach (ChoIniDocumentState iniDocumentState in e.IniDocumentStates)
            {
                ChoProfile.RegisterIfNotExists(iniDocumentState.IniFilePath, new ChoBufferProfileEx(ChoFileProfileSettings.GetFullPath(ChoReservedDirectoryName.Others,
                                                                                                                                       ChoPath.AddExtension(Path.GetFileName(iniDocumentState.IniFilePath), ChoReservedFileExt.Ignore)),
                                                                                                    "Entries Ignored are..."));
                ChoProfile.GetContext(iniDocumentState.IniFilePath).AppendLine(iniDocumentState.ToString());
            }
        }
        public void Initialize(bool beforeFieldInit)
        {
            if (ConfigNameValues == null || ConfigNameValues.Length == 0)
            {
                return;
            }
            if (String.IsNullOrEmpty(Name))
            {
                ChoProfile.WriteLine("Missing Name");
                return;
            }

            using (ChoBufferProfile profile = ChoBufferProfile.DelayedAutoStart(new ChoBufferProfile(true, Name, "Loading property dictionary...")))
            {
                foreach (ChoPropertyNameValue configNameValue in ConfigNameValues)
                {
                    if (configNameValue == null)
                    {
                        continue;
                    }
                    if (String.IsNullOrEmpty(configNameValue.Name))
                    {
                        continue;
                    }

                    try
                    {
                        _properties.Add(configNameValue.Name, ChoString.ToObject(configNameValue.Value));
                    }
                    catch (Exception ex)
                    {
                        profile.AppendLine(String.Format("{0}: {1}", configNameValue.Name, ex.ToString()));
                    }
                }
            }
            ConfigNameValues = null;
        }
Ejemplo n.º 8
0
        public void Initialize()
        {
            if (_propertyReplacers == null)
            {
                _propertyReplacers = new ChoDictionary <string, IChoPropertyReplacer>(DefaultPropertyReplacers);
            }

            if (_propertyReplacers.Count == DefaultPropertyReplacers.Count)
            {
                if (PropertyDictionaryReplacers != null)
                {
                    foreach (ChoObjConfigurable objConfigurable in PropertyDictionaryReplacers)
                    {
                        if (objConfigurable == null)
                        {
                            continue;
                        }
                        if (String.IsNullOrEmpty(objConfigurable.Name))
                        {
                            continue;
                        }
                        if (!(objConfigurable is IChoKeyValuePropertyReplacer))
                        {
                            continue;
                        }

                        if (_propertyReplacers.ContainsKey(objConfigurable.Name))
                        {
                            ChoProfile.WriteLine(String.Format("Item with {0} key already exists.", objConfigurable.Name));
                            continue;
                        }

                        _propertyReplacers.Add(objConfigurable.Name, objConfigurable as IChoPropertyReplacer);
                    }
                }
                if (CustomPropertyReplacers != null)
                {
                    foreach (ChoObjConfigurable objConfigurable in CustomPropertyReplacers)
                    {
                        if (objConfigurable == null)
                        {
                            continue;
                        }
                        if (String.IsNullOrEmpty(objConfigurable.Name))
                        {
                            continue;
                        }
                        if (!(objConfigurable is IChoCustomPropertyReplacer))
                        {
                            continue;
                        }

                        if (_propertyReplacers.ContainsKey(objConfigurable.Name))
                        {
                            ChoProfile.WriteLine(String.Format("Item with {0} key already exists.", objConfigurable.Name));
                            continue;
                        }

                        _propertyReplacers.Add(objConfigurable.Name, objConfigurable as IChoPropertyReplacer);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        private void WaitFor(Delegate func, object[] parameters, int timeout, int maxNoOfRetry, int sleepBetweenRetry, ChoExecutionServiceData data)
        {
            ChoGuard.ArgumentNotNull(func, "Function");

            ChoAbortableAsyncResult asyncResult = data.Result as ChoAbortableAsyncResult;

            ChoWaitFor.CheckParams(timeout, maxNoOfRetry, sleepBetweenRetry);

            int    noOfRetry = 0;
            object retValue  = null;
            ChoList <Exception> aggExceptions = new ChoList <Exception>();
            Func <object>       wrappedFunc   = delegate
            {
                if (asyncResult.AbortRequested)
                {
                    asyncResult.SetAsAborted(true);
                }

                asyncResult.ThreadToKill = Thread.CurrentThread;
                return(func.DynamicInvoke(parameters));
            };

            while (true)
            {
                if (asyncResult.AbortRequested)
                {
                    asyncResult.SetAsAborted(true);
                }

                try
                {
                    if (timeout == Timeout.Infinite)
                    {
                        retValue = wrappedFunc.Invoke();
                    }
                    else
                    {
                        IAsyncResult result = wrappedFunc.BeginInvoke(null, null);
                        Thread.Sleep(1000);
                        using (result.AsyncWaitHandle)
                        {
                            if (!result.AsyncWaitHandle.WaitOne(timeout, true))
                            {
                                if (asyncResult.ThreadToKill != null && asyncResult.ThreadToKill.IsAlive)
                                {
                                    asyncResult.ThreadToKill.AbortThread();
                                }

                                ChoWaitFor.RaiseTimeoutException(func, timeout);
                            }
                            else
                            {
                                try
                                {
                                    retValue = wrappedFunc.EndInvoke(result);
                                }
                                catch (ThreadAbortException)
                                {
                                    //Thread.ResetAbort();
                                    asyncResult.SetAsAborted(true);
                                    return;
                                }
                            }
                        }
                    }
                    asyncResult.SetAsSuccess(retValue, true);
                    return;
                }
                catch (ThreadAbortException)
                {
                    Thread.ResetAbort();
                    ChoProfile.WriteLine("Thread Aborted.");
                    ChoProfile.WriteLine(data.ToString());
                    asyncResult.SetAsAborted(true);
                    break;
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (maxNoOfRetry != 0)
                    {
                        if (noOfRetry == maxNoOfRetry)
                        {
                            asyncResult.SetAsFailed(new ChoAggregateException(String.Format("The method failed to execute after {0} retries.", maxNoOfRetry), aggExceptions), true);
                            return;
                        }

                        noOfRetry++;
                        aggExceptions.Add(ex);

                        Thread.Sleep((int)sleepBetweenRetry);
                    }
                    else
                    {
                        ChoProfile.WriteLine(ex.ToString());
                        ChoProfile.WriteLine(data.ToString());
                        asyncResult.SetAsFailed(ex.InnerException != null ? ex.InnerException : ex, true);
                        return;
                    }
                }
            }

            //if (asyncResult.AbortRequested)
            //    asyncResult.SetAsAborted(true);

            //Action wrappedFunc1 = delegate
            //{
            //    asyncResult.ThreadToKill = Thread.CurrentThread;

            //    while (true)
            //    {
            //        if (asyncResult.AbortRequested)
            //        {
            //            asyncResult.SetAsAborted(true);
            //            break;
            //        }

            //        try
            //        {
            //            if (timeout == Timeout.Infinite)
            //                retValue = func.DynamicInvoke(parameters);
            //            else
            //            {
            //                IAsyncResult result = wrappedFunc.BeginInvoke(null, null);
            //                Thread.Sleep(1000);
            //                using (result.AsyncWaitHandle)
            //                {
            //                    if (!result.AsyncWaitHandle.WaitOne(timeout, true))
            //                    {
            //                        try
            //                        {
            //                            if (asyncResult.ThreadToKill != null && asyncResult.ThreadToKill.IsAlive)
            //                                asyncResult.ThreadToKill.Abort();
            //                        }
            //                        catch (ThreadAbortException)
            //                        {
            //                            Thread.ResetAbort();
            //                        }

            //                        ChoWaitFor.RaiseTimeoutException(func, timeout);
            //                    }
            //                    else
            //                        retValue = wrappedFunc.EndInvoke(result);
            //                }
            //            }
            //            asyncResult.SetAsSuccess(retValue, true);
            //            return;
            //        }
            //        catch (ThreadAbortException)
            //        {
            //            Thread.ResetAbort();
            //            ChoProfile.WriteLine("Thread Aborted.");
            //            ChoProfile.WriteLine(data.ToString());
            //            asyncResult.SetAsAborted(true);
            //            break;
            //        }
            //        catch (Exception ex)
            //        {
            //            if (maxNoOfRetry != 0)
            //            {
            //                if (noOfRetry == maxNoOfRetry)
            //                {
            //                    asyncResult.SetAsFailed(new ChoAggregateException(String.Format("The method failed to execute after {0} retries.", maxNoOfRetry), aggExceptions), true);
            //                    return;
            //                }

            //                noOfRetry++;
            //                aggExceptions.Add(ex);

            //                Thread.Sleep((int)sleepBetweenRetry);
            //            }
            //            else
            //            {
            //                ChoProfile.WriteLine(ex.ToString());
            //                ChoProfile.WriteLine(data.ToString());
            //                asyncResult.SetAsFailed(ex.InnerException != null ? ex.InnerException : ex, true);
            //                return;
            //            }
            //        }
            //    }
            //};

            //IAsyncResult r = wrappedFunc1.BeginInvoke(null, null);
            //wrappedFunc1.EndInvoke(r);
        }
Ejemplo n.º 10
0
        public bool Initialize(bool beforeFieldInit, object state)
        {
            if (_propertyReplacers == null)
            {
                //ChoStreamProfile.Clean(ChoReservedDirectoryName.Settings, ChoType.GetLogFileName(typeof(ChoPropertyManagerSettings), ChoReservedFileExt.Err));
                _propertyReplacers = new ChoDictionary <string, IChoPropertyReplacer>(DefaultPropertyReplacers);
            }

            if (!beforeFieldInit)
            {
                if (_propertyReplacers.Count == DefaultPropertyReplacers.Count)
                {
                    if (PropertyDictionaryReplacers != null)
                    {
                        foreach (ChoObjConfigurable objConfigurable in PropertyDictionaryReplacers)
                        {
                            if (objConfigurable == null)
                            {
                                continue;
                            }
                            if (String.IsNullOrEmpty(objConfigurable.Name))
                            {
                                continue;
                            }
                            if (!(objConfigurable is IChoKeyValuePropertyReplacer))
                            {
                                continue;
                            }

                            if (_propertyReplacers.ContainsKey(objConfigurable.Name))
                            {
                                ChoProfile.WriteLine(String.Format("Item with {0} key already exists.", objConfigurable.Name));
                                continue;
                            }

                            _propertyReplacers.Add(objConfigurable.Name, objConfigurable as IChoPropertyReplacer);
                        }
                    }
                    if (CustomPropertyReplacers != null)
                    {
                        foreach (ChoObjConfigurable objConfigurable in CustomPropertyReplacers)
                        {
                            if (objConfigurable == null)
                            {
                                continue;
                            }
                            if (String.IsNullOrEmpty(objConfigurable.Name))
                            {
                                continue;
                            }
                            if (!(objConfigurable is IChoCustomPropertyReplacer))
                            {
                                continue;
                            }

                            if (_propertyReplacers.ContainsKey(objConfigurable.Name))
                            {
                                ChoProfile.WriteLine(String.Format("Item with {0} key already exists.", objConfigurable.Name));
                                continue;
                            }

                            _propertyReplacers.Add(objConfigurable.Name, objConfigurable as IChoPropertyReplacer);
                        }
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 11
0
 internal static string FormatException(string expr, Exception ex)
 {
     ChoProfile.WriteLine("Expression: {1}{0}Error: {2}", Environment.NewLine, expr, ex.ToString());
     return(String.Format("{2} Error while evaluating `{0}' expression. {1}]", expr, ex.Message, ChoString.ExceptionStringToken));
 }