コード例 #1
0
        public int CfRunLoopStart()
        {
            //還在執行中, 不接受重新執行
            if (this.RunningTask != null && this.RunningTask.Status < TaskStatus.RanToCompletion)
            {
                return(1);
            }

            //需要重複確認機台的功能是活著的, 因此使用RunLoop重複運作
            this.RunningTask = CtkTask.RunLoop(() =>
            {
                try
                {
                    this.CfRunOnce();
                }
                catch (Exception ex)
                {
                    //Task root method 需要try/catch: 已經到最上層, 就必須捕抓->寫log, 否則就看不到這個例外
                    CtkLog.WriteNs(this, ex);
                }
                Thread.Sleep(5 * 1000);
                return(!this.disposed);//d20201210 機台仍舊要持續運作
            });

            return(0);
        }
コード例 #2
0
 public static void DisposeTask(CtkTask task, int?millisecond = null)
 {
     if (task == null)
     {
         return;
     }
     if (millisecond.HasValue)
     {
         task.DisposeWaitTime = millisecond.Value;
     }
     task.Dispose();
 }
コード例 #3
0
 public static bool DisposeTaskTry(CtkTask task, int?millisecond = null)
 {
     try
     {
         DisposeTask(task, millisecond);
         return(true);
     }
     catch (Exception ex)
     {
         CtkLog.Warn(ex);
         return(false);
     }
 }
コード例 #4
0
 public virtual int CfRunLoopStart()
 {
     if (this.taskRun != null)
     {
         if (!this.taskRun.Wait(100))
         {
             return(0);                        //正在工作
         }
     }
     //the CfRun is loop function
     this.taskRun = CtkTask.RunOnce((ct) => { this.CfRunLoop(); });
     return(0);
 }
コード例 #5
0
        public void NonStopRunStart()
        {
            this.NonStopRunStop();
            this.runningTask = CtkTask.RunOnce(() =>
            {
                //TODO: 重啟時, 會有執行緒被中止的狀況
                while (!this.disposed)
                {
                    try
                    {
                        this.ConnectTryStart();
                    }
                    catch (Exception ex) { CtkLog.Write(ex); }

                    Thread.Sleep(this.IntervalTimeOfConnectCheck);
                }
            });
        }
コード例 #6
0
        public void NonStopRunStart()
        {
            NonStopRunStop();

            this.runningTask = CtkTask.RunOnce((ct) =>
            {
                while (!this.disposed && !ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                    try
                    {
                        this.ConnectTry();
                    }
                    catch (Exception ex) { CtkLog.Write(ex); }
                    Thread.Sleep(this.IntervalTimeOfConnectCheck);
                }
            });
        }
コード例 #7
0
        protected virtual void WriteAsyn(CtkLoggerEventArgs ea)
        {
            this.queue.Enqueue(ea);
            if (!Monitor.TryEnter(this, 1000))
            {
                return;
            }
            try
            {
                if (this.task != null)
                {
                    //若還沒結束執行, 先return
                    if (!this.task.IsEnd())
                    {
                        return;
                    }
                    //若之前有, 把它清乾淨
                    using (var obj = this.task)
                        if (!obj.IsEnd())
                        {
                            obj.Cancel();
                        }
                }


                this.task = CtkTask.RunLoop(() =>
                {
                    CtkLoggerEventArgs myea;
                    lock (this)
                    {
                        if (!this.queue.TryDequeue(out myea))
                        {
                            return(true);                                 //取不出來就下次再取
                        }
                    }
                    this.WriteSyn(myea);

                    //若Count等於零, 這個task會結束, IsEnd() = true
                    return(this.queue.Count > 0);
                });
            }
            finally { Monitor.Exit(this); }
        }
コード例 #8
0
        public void Close()
        {
            this.CfIsRunning = false;
            this.areMsg.Set();//若在等訊號也通知結束等待

            if (this.taskRun != null)
            {
                this.taskRun.Cancel();//取消執行Task
                this.taskRun.Wait(1000);
                this.taskRun.Dispose();
                this.taskRun = null;
            }
            if (this.ProtoConn != null)
            {
                this.ProtoConn.Disconnect();
                this.ProtoConn.Dispose();
                this.ProtoConn = null;
            }
            CtkEventUtil.RemoveEventHandlersOfOwnerByFilter(this, (dlgt) => true);
        }
コード例 #9
0
 public void NonStopRunStop()
 {
     CtkUtil.DisposeTaskTry(this.runningTask);
     this.runningTask = null;
 }