/// <summary>
 /// Função que inicializa as operações da thread. Quando o contrutor EasyThread(EasyThreadFun, bool, object) é utilizado, esta função é executada automaticamente.
 /// </summary>
 /// <param name="fun">Função a ser executada</param>
 /// <param name="runAsWhileTrue">Indica se a thread deve chamar a função "fun" em um "while true". Caso seja true, a função é executada repetidamente.</param>
 /// <param name="thParams">Parametros que serão passados para a função "fun"</param>
 public void Start(EasyThreadFun fun, bool runAsWhileTrue, object thParams = null)
 {
     if (this.canRun())
     {
         thread = new Thread(delegate()
         {
             this.__status = ThreadStatus.running;
             if (runAsWhileTrue)
             {
                 while (this.canRun())
                 {
                     if (!__pause)
                     {
                         fun(this, thParams);
                     }
                     else
                     {
                         this.sleep(1);
                     }
                 }
                 this.__status = ThreadStatus.exited;
             }
             else
             {
                 fun(this, thParams);
                 this.__status = ThreadStatus.exited;
             }
         });
         thread.Start();
     }
 }
Пример #2
0
        /// <summary>
        /// Starts the threads.
        /// </summary>
        /// <param name="fun">The thread function</param>
        /// <param name="runAsWhileTrue">Run the thread inside a while true.</param>
        /// <param name="thParams">Arguments that will passed in each "fun" call/param>
        public void Start(EasyThreadFun fun, bool runAsWhileTrue, object thParams = null, EasyThreadFun onEnd = null, ThreadPriority priority = ThreadPriority.Normal, int interval = -1)
        {
            this.onEnd = onEnd;

            if (this.canRun())
            {
                thread = new Thread(delegate()
                {
                    this.__status = ThreadStatus.running;
                    if (runAsWhileTrue)
                    {
                        while (this.canRun())
                        {
                            if (!__pause)
                            {
                                fun(this, thParams);
                                if (interval > -1)
                                {
                                    Thread.Sleep(interval);
                                }
                            }
                            else
                            {
                                this.sleep(1);
                            }
                        }
                        this.__status = ThreadStatus.exited;
                    }
                    else
                    {
                        fun(this, thParams);
                        this.__status = ThreadStatus.exited;
                    }
                    if (this.onEnd != null)
                    {
                        this.onEnd(this, thParams);
                    }
                });
                thread.Priority = priority;
                thread.Start();
            }
        }
Пример #3
0
 /// <summary>
 /// Create a Thread auto timerized, like a timer. The minimun time precision is 10 milisseconds. Can be used when a long interval ins needed.
 /// The timer can be stopped in any moment.
 /// </summary>
 /// <param name="fun">The function to be called</param>
 /// <param name="interval">The interval to execute function (the minimum value is 10 milisseconds)</param>
 /// <param name="thParams">Optional parameters to be passed in each "fun" call</param>
 /// <param name="onEnd">A optional function to be called when the timer is stopped</param>
 /// <param name="priority">The thread priority.</param>
 /// <returns></returns>
 public static EasyThread StartTimer(EasyThreadFun fun, int interval, object thParams = null, EasyThreadFun onEnd = null, ThreadPriority priority = ThreadPriority.Normal)
 {
     return(StartNew(delegate(EasyThread snd, object args)
     {
         if (snd.Tags.ContainsKey("timeout"))
         {
             Thread.Sleep(10);
             if (DateTime.Now.Subtract((DateTime)snd.Tags["timeout"]).TotalMilliseconds >= interval)
             {
                 snd.Tags["timeout"] = DateTime.Now;
                 fun(snd, args);
             }
         }
         else
         {
             snd.Tags["timeout"] = DateTime.Now;
             fun(snd, args);
         }
     }, true, thParams, onEnd, priority));
 }
Пример #4
0
 /// <summary>
 /// Contructor that auto starts the thread.
 /// </summary>
 /// <param name="fun">The thread function</param>
 /// <param name="runAsWhileTrue">Run the thread inside a while true.</param>
 /// <param name="thParams">Arguments that will passed in each "fun" call/param>
 public EasyThread(EasyThreadFun fun, bool runAsWhileTrue = true, object thParams = null, EasyThreadFun onEnd = null, ThreadPriority priority = ThreadPriority.Normal, int interval = -1)
 {
     EasyThread.threadList.Add(this);
     this.Start(fun, runAsWhileTrue, thParams, onEnd, priority, interval);
 }
Пример #5
0
 /// <summary>
 /// Cria um novo objeto EasyThread
 /// </summary>
 /// <param name="fun">Função a ser execuata</param>
 /// <param name="runAsWhileTrue">Indica se deve executar como um while true</param>
 /// <param name="thParams">Parametros que serão passados para a thread</param>
 /// <returns></returns>
 public static EasyThread StartNew(EasyThreadFun fun, bool runAsWhileTrue, object thParams = null, EasyThreadFun onEnd = null, ThreadPriority priority = ThreadPriority.Normal)
 {
     return(new EasyThread(fun, runAsWhileTrue, thParams, onEnd, priority));
 }
 /// <summary>
 /// Construtor que já inicializa as operações da thread.
 /// </summary>
 /// <param name="fun">Função a ser executada</param>
 /// <param name="runAsWhileTrue">Indica se a thread deve chamar a função "fun" em um "while true". Caso seja true, a função é executada repetidamente.</param>
 /// <param name="thParams">Parametros que serão passados para a função "fun"</param>
 public EasyThread(EasyThreadFun fun, bool runAsWhileTrue, object thParams = null)
 {
     EasyThread.threadList.Add(this);
     this.Start(fun, runAsWhileTrue, thParams);
 }
 /// <summary>
 /// Cria um novo objeto EasyThread
 /// </summary>
 /// <param name="fun">Função a ser execuata</param>
 /// <param name="runAsWhileTrue">Indica se deve executar como um while true</param>
 /// <param name="thParams">Parametros que serão passados para a thread</param>
 /// <returns></returns>
 public static EasyThread StartNew(EasyThreadFun fun, bool runAsWhileTrue, object thParams = null)
 {
     return(new EasyThread(fun, runAsWhileTrue, thParams));
 }