示例#1
0
        public bool RunParallel(Expression <Action> EAMethod, int interval, int delay, string ID, bool waitForAccess)
        {
            if (System.String.IsNullOrEmpty(ID))
            {
                ID = Expressions.nameofFull(EAMethod);
            }

            return(Methods.Run(() => this.Run(EAMethod, interval, ID, waitForAccess, true), ID, delay, true, true));
        }
示例#2
0
        public bool Run(Expression <Action> EAMethod, int interval, int delay, string ID, bool waitForAccess)
        {
            if (System.String.IsNullOrEmpty(ID))
            {
                ID = Expressions.nameofFull(EAMethod);
            }

            if (Threads == null)
            {
                Threads = new ThreadedMethod(this.MaxThreadsCount, ThreadPriority.Lowest, 1);
            }


            Threads.Run(() => this.Run(EAMethod, interval, ID, waitForAccess), ID, delay, true, true, false);

            return(false);
        }
示例#3
0
        /// <summary>
        /// Autostarts timer
        /// </summary>
        /// <param name="EAMethod"></param>
        /// <param name="interval"></param>
        /// <param name="ID"></param>
        /// <param name="Exceptions"></param>
        /// <param name="waitForAccess"></param>
        /// <returns></returns>
        public bool Run(Expression <Action> EAMethod, int interval, string ID = null, bool Exceptions = true, bool waitForAccess = false)
        {
            if (EAMethod == null)
            {
                return(false);
            }

            if (MaxThreadsCount < TDSTTimers.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }

                while (MaxThreadsCount <= TDSTTimers.Count)
                {
                    Thread.Sleep(1);
                }
            }


            if (System.String.IsNullOrEmpty(ID))
            {
                ID = Expressions.nameofFull(EAMethod);
            }

            ThreadedTimer TTimer = Timer(ID);

            if (TTimer != null && TTimer.Enabled)
            {
                return(false);
            }

            TTimer = new ThreadedTimer(EAMethod, interval, true);

            TDSTTimers.Add(ID, TTimer);
            TDSTTimers[ID].Start();

            return(true);


            // return this.Run(EAMethod, interval, ID, Exceptions, waitForAccess, true);
        }
示例#4
0
        public bool Run(Expression <Action> EAMethod, int interval, string ID = null, bool waitForAccess = true, bool autostart = true)
        {
            if (EAMethod == null)
            {
                return(false);
            }

            if (MaxThreadsCount < Workers.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }

                while (MaxThreadsCount <= Workers.Count)
                {
                    Thread.Sleep(1);
                }
            }

            if (ID.IsNullOrEmpty())
            {
                ID = Expressions.nameofFull(EAMethod);
            }

            lock (locker)
            {
                ThreadedTimer TTimer = Workers.ContainsKey(ID) ? Workers[ID] : null;

                if (TTimer != null && TTimer.Enabled)
                {
                    return(false);
                }

                TTimer = new ThreadedTimer(EAMethod, interval, autostart);

                Workers.Add(ID, TTimer, true);
                Workers[ID].Start();
            }

            return(true);
        }
示例#5
0
        public bool Run(Expression <Action> EAMethod, int interval, string ID, bool waitForAccess, bool autostart)
        {
            if (EAMethod == null)
            {
                return(false);
            }

            if (MaxThreadsCount < TDSTTimers.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }

                while (MaxThreadsCount <= TDSTTimers.Count)
                {
                    Thread.Sleep(1);
                }
            }


            if (ID.IsNullOrEmpty())
            {
                ID = Expressions.nameofFull(EAMethod);
            }

            ThreadedTimer TTimer = Timer(ID);

            if (TTimer != null && TTimer.Enabled)
            {
                return(false);
            }

            TTimer = new ThreadedTimer(EAMethod, interval, autostart);

            TDSTTimers.Add(ID, TTimer);
            TDSTTimers[ID].Start();

            return(true);
        }
示例#6
0
 public void Terminate(Expression <Action> EAMethod)
 {
     Terminate(Expressions.nameofFull(EAMethod));
 }
示例#7
0
        public bool RunF <TResult>(Expression <Func <TResult> > expression, string ID, int delay, bool Exceptions, bool waitForAccess, bool invoke)
        {
            if (expression == null)
            {
                return(false);
            }


            while (MaxThreadsCount < TDSThreads.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }
                ;

                Thread.Sleep(AccessWait);
                this.TerminateAllCompleated();
            }

            if (System.String.IsNullOrEmpty(ID))
            {
                ID = Expressions.nameofFull <TResult>(expression);
            }
            if (IsAlive(ID))
            {
                return(false);
            }
            try
            {
                lock (Locker.Get("Run"))
                {
                    this.Terminate(ID);

                    Func <TResult> Function = expression.Compile();
                    Results.Add(ID, default(TResult));

                    if (Exceptions)
                    {
                        if (invoke)
                        {
                            TDSThreads.Add(ID, new Thread(() => { if (delay > 0)
                                                                  {
                                                                      Thread.Sleep(delay);
                                                                  }
                                                                  TResult result = Function.Invoke(); Results[ID] = result; }));
                        }
                        else
                        {
                            TDSThreads.Add(ID, new Thread(() => { if (delay > 0)
                                                                  {
                                                                      Thread.Sleep(delay);
                                                                  }
                                                                  TResult result = Function(); Results[ID] = result; }));
                        }
                    }
                    else
                    {
                        if (invoke)
                        {
                            TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0)
                                                                        {
                                                                            Thread.Sleep(delay);
                                                                        }
                                                                        TResult result = Function.Invoke(); Results[ID] = result; } catch { } }));
                        }
                        else
                        {
                            TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0)
                                                                        {
                                                                            Thread.Sleep(delay);
                                                                        }
                                                                        TResult result = Function(); Results[ID] = result; } catch { } }));
                        }
                    }


                    TDSThreads[ID].Priority     = Priority;
                    TDSThreads[ID].IsBackground = true;
                    TDSThreads[ID].Start();
                }
            }
            finally
            {
            }

            return(true);
        }
示例#8
0
        public bool Run(Expression <Action> EAMethod, string ID, int delay, bool Exceptions, bool waitForAccess, bool invoke)
        {
            if (EAMethod == null)
            {
                return(false);
            }


            while (MaxThreadsCount < TDSThreads.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }

                Thread.Sleep(AccessWait);
                this.TerminateAllCompleated();
            }

            if (System.String.IsNullOrEmpty(ID))
            {
                ID = Expressions.nameofFull(EAMethod);
            }
            if (IsAlive(ID))
            {
                return(false);
            }
            try
            {
                lock (Locker.Get("Run"))
                {
                    this.Terminate(ID);

                    Action Action = EAMethod.Compile();

                    if (Exceptions)
                    {
                        if (invoke)
                        {
                            TDSThreads.Add(ID, new Thread(() => { if (delay > 0)
                                                                  {
                                                                      Thread.Sleep(delay);
                                                                  }
                                                                  Action.Invoke(); }));
                        }
                        else
                        {
                            TDSThreads.Add(ID, new Thread(() => { if (delay > 0)
                                                                  {
                                                                      Thread.Sleep(delay);
                                                                  }
                                                                  Action(); }));
                        }
                    }
                    else
                    {
                        if (invoke)
                        {
                            TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0)
                                                                        {
                                                                            Thread.Sleep(delay);
                                                                        }
                                                                        Action.Invoke(); } catch { } }));
                        }
                        else
                        {
                            TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0)
                                                                        {
                                                                            Thread.Sleep(delay);
                                                                        }
                                                                        Action(); } catch { } }));
                        }
                    }


                    TDSThreads[ID].Priority     = Priority;
                    TDSThreads[ID].IsBackground = true;
                    TDSThreads[ID].Start();
                }
            }
            catch (Exception e)
            {
                if (e.Message != null)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#9
0
文件: Run.cs 项目: jeason0813/Asmodat
        public bool RunF <TResult>(Expression <Func <TResult> > expression, string ID, int delay, bool Exceptions, bool waitForAccess)
        {
            if (expression == null)
            {
                return(false);
            }


            while (MaxThreadsCount < TDSThreads.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }

                Thread.Sleep(AccessWait);
                this.TerminateAllCompleated();
            }

            if (ID.IsNullOrEmpty())
            {
                ID = Expressions.nameofFull <TResult>(expression);
            }

            if (IsAlive(ID))
            {
                return(false);
            }

            try
            {
                if (!LockerRun.EnterWait())
                {
                    return(false);
                }

                this.Terminate(ID);

                Func <TResult> Function = expression.Compile();
                Results.Add(ID, default(TResult), true);

                if (Exceptions)
                {
                    TDSThreads.Add(ID, new Thread(() => { if (delay > 0)
                                                          {
                                                              Thread.Sleep(delay);
                                                          }
                                                          TResult result = Function(); Results[ID] = result; }), true);
                }
                else
                {
                    TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0)
                                                                {
                                                                    Thread.Sleep(delay);
                                                                }
                                                                TResult result = Function(); Results[ID] = result; } catch { } }), true);
                }


                TDSTMFlags.Add(ID, new ThreadedMethodFlags {
                    IsAborting = false
                }, true);

                TDSThreads[ID].Priority     = Priority;
                TDSThreads[ID].IsBackground = true;
                TDSThreads[ID].Start();
            }
            finally
            {
                LockerRun.Exit();
            }

            return(true);
        }
示例#10
0
文件: Run.cs 项目: jeason0813/Asmodat
        public bool Run(Expression <Action> EAMethod, string ID, int delay, bool Exceptions, bool waitForAccess)
        {
            if (EAMethod == null)
            {
                return(false);
            }

            while (MaxThreadsCount < TDSThreads.Count)
            {
                if (!waitForAccess)
                {
                    return(false);
                }

                Thread.Sleep(AccessWait);
                this.TerminateAllCompleated();
            }

            if (ID.IsNullOrEmpty())
            {
                ID = Expressions.nameofFull(EAMethod);
            }

            if (IsAlive(ID))
            {
                return(false);
            }

            try
            {
                if (!LockerRun.EnterWait())
                {
                    return(false);
                }

                this.Terminate(ID);

                Action Action = EAMethod.Compile();

                if (Exceptions)
                {
                    TDSThreads.Add(ID, new Thread(() => { if (delay > 0)
                                                          {
                                                              Thread.Sleep(delay);
                                                          }
                                                          Action(); }), true);
                }
                else
                {
                    TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0)
                                                                {
                                                                    Thread.Sleep(delay);
                                                                }
                                                                Action(); } catch { } }), true);
                }

                TDSTMFlags.Add(ID, new ThreadedMethodFlags {
                    IsAborting = false
                }, true);

                TDSThreads[ID].Priority     = Priority;
                TDSThreads[ID].IsBackground = true;
                TDSThreads[ID].Start();
            }
            catch (Exception e)
            {
                if (e.Message != null)
                {
                    return(false);
                }
            }
            finally
            {
                LockerRun.Exit();
            }

            return(true);
        }
示例#11
0
 public bool Terminate(Expression <Action> EAMethod)
 {
     return(Terminate(Expressions.nameofFull(EAMethod)));
 }
示例#12
0
 public bool Contains(Expression <Action> EAMethod)
 {
     return(Contains(Expressions.nameofFull(EAMethod)));
 }
示例#13
0
 public ThreadedTimer Timer(Expression <Action> EAMethod)
 {
     return(Timer(Expressions.nameofFull(EAMethod)));
 }
示例#14
0
 public bool IsAlive(Expression <Action> EAMethod)
 {
     return(IsAlive(Expressions.nameofFull(EAMethod)));
 }
示例#15
0
 public void Join(Expression <Action> EAMethod, int timeout = int.MaxValue)
 {
     Join(Expressions.nameofFull(EAMethod), timeout);
 }
示例#16
0
 public TResult JoinF <TResult>(Expression <Func <TResult> > expression, int timeout = int.MaxValue)
 {
     return(JoinF <TResult>(Expressions.nameofFull <TResult>(expression), timeout));
 }