Beispiel #1
0
        /// <summary>
        /// 开始启动服务
        /// </summary>
        public void Start()
        {
            if (ThreadStarting != null)
            {
                ThreadSvrUtilEventArgs e = new ThreadSvrUtilEventArgs();
                ThreadStarting(this, e);
                if (e.Cancel)
                {
                    return;           //中止执行
                }
            }

            Runing = true;
            if (threadSync != null && threadSync.ThreadState == ThreadState.Running)
            {
                return;
            }
            else if (threadSync != null && !(threadSync.ThreadState == ThreadState.Stopped || threadSync.ThreadState == ThreadState.Aborted || threadSync.ThreadState == ThreadState.AbortRequested))
            {
                try { threadSync.Abort(); }
                catch { }
            }
            threadSync = new Thread(new ThreadStart(ThreadMethod));
            IsSuspend  = false;
            Runing     = true;
            threadSync.Start();//开始进行线程服务
            if (this.ThreadStarted != null)
            {
                this.ThreadStarted(this, new EventArgs());
            }
        }
Beispiel #2
0
        /// <summary>
        /// 停止运行服务
        /// </summary>
        public void Stop()
        {
            if (ThreadStoping != null)
            {
                ThreadSvrUtilEventArgs e = new ThreadSvrUtilEventArgs();
                ThreadStoping(this, e);
                if (e.Cancel)
                {
                    return;           //中止执行
                }
            }
            IsSuspend = false;
            Runing    = false;

            if (threadSync == null || threadSync.ThreadState == ThreadState.Aborted || threadSync.ThreadState == ThreadState.Stopped || threadSync.ThreadState == ThreadState.AbortRequested)
            {
                return;
            }
            //中止当前线程
            try {
                threadSync.Join(1500);
                threadSync.Abort();
            }
            catch { }
            threadSync = null;

            if (this.ThreadStoped != null)
            {
                this.ThreadStoped(this, new EventArgs());
            }
        }