Пример #1
0
            public static IAsyncResult BeginInvoke(Delegate method, TimeSpan?delay, TimeSpan?minSpan, params object[] args)
            {
                delay ??= TimeSpan.Zero;
                minSpan ??= TimeSpan.Zero;

                Action action =
                    () =>
                {
                    var watch = new Stopwatch();
                    watch.Start();
                    Thread.Sleep(delay.Value);
                    try
                    {
                        method.DynamicInvoke(args);
                    }
                    catch (Exception ex)
                    {
                        XDebug.OnException(ex);

                        throw;
                    }

                    var elapsed = minSpan.Value - watch.Elapsed;
                    watch.Stop();
                    if (elapsed.TotalSeconds > 0)
                    {
                        Thread.Sleep(elapsed);
                    }
                };

                return(action.BeginInvoke(action.EndInvoke, null));
            }
Пример #2
0
        protected void InternalRunTask()
        {
            Task.Run(
                () =>
            {
                Thread.CurrentThread.Name = Name;

                var item = default(T);
                do
                {
                    try
                    {
                        item = _readData();

                        if (item != null)
                        {
                            _processData(item);
                        }
                    }
                    catch (Exception ex)
                    {
                        XDebug.OnException(ex);

                        if (!AutoCatchException)
                        {
                            throw;
                        }
                    }

                    if (IsContinuous)
                    {
                        // ReSharper disable PossibleInvalidOperationException
                        var waitForSeconds = (int)Math.Max(InternalGetFrequency().Value.TotalSeconds, 1);
                        // ReSharper restore PossibleInvalidOperationException

                        for (var i = 0; i < waitForSeconds; i++)
                        {
                            Thread.Sleep(1000);

                            if (_stopping)
                            {
                                break;
                            }
                        }
                    }
                } while ((!Equals(item, null) || IsContinuous) && !_stopping);

                lock (_syncRunning)
                {
                    _active   = false;
                    _stopping = false;

                    _waitStop.Set();
                }
            });
        }
Пример #3
0
            public static bool Ensure(Action action)
            {
                try
                {
                    action();

                    return(true);
                }
                catch (Exception e)
                {
                    XDebug.OnException(e);

                    return(true);
                }
            }
Пример #4
0
            public static bool Ensure(Action action, out string error)
            {
                try
                {
                    action();

                    error = null;

                    return(true);
                }
                catch (Exception e)
                {
                    XDebug.OnException(e);

                    error = Exceptions.GetMessageOnTopOfStack(e);
                    return(true);
                }
            }
Пример #5
0
            public static void TryDelete(params string[] files)
            {
                foreach (var file in files)
                {
                    try
                    {
                        if (!Strings.HasData(file))
                        {
                            continue;
                        }

                        File.Delete(file);
                    }
                    catch (Exception e)
                    {
                        XDebug.OnException(e);
                    }
                }
            }
Пример #6
0
        public TimerActionTask Binding(Action action)
        {
            XContract.ArgIsNotNull(action, nameof(action));

            var localAction = action;

            _timer.Elapsed +=
                (sender, args) =>
            {
                var restart = true;
                try
                {
                    localAction();

                    _timer.Interval = NormalFrequence.TotalMilliseconds;
                }
                catch (Exception ex)
                {
                    XDebug.OnException(ex);

                    if (!AutoCatchException)
                    {
                        restart = false;

                        throw;
                    }

                    ExceptionHandled?.Invoke(this, ex);

                    _timer.Interval = ErrorFrequence.TotalMilliseconds;
                }
                finally
                {
                    if (restart)
                    {
                        _timer.Start();
                    }
                }
            };

            return(this);
        }
Пример #7
0
                public static Task Run(object context, string alias, Action actionToRun)
                {
                    XContract.ArgIsNotNull(context, nameof(context));
                    XContract.ArgIsNotNull(alias, nameof(alias));
                    XContract.ArgIsNotNull(actionToRun, nameof(actionToRun));

                    return(Task.Run(
                               () =>
                    {
                        Thread.CurrentThread.Name = CreateTheadName(context, alias);

                        try
                        {
                            actionToRun();
                        }
                        catch (Exception ex)
                        {
                            XDebug.OnException(ex);

                            throw;
                        }
                    }));
                }