Esempio n. 1
0
 internal void RaiseOnStop(string sender = null, XBMCRPC.Player.OnStop_data data = null)
 {
     if (OnStop != null)
     {
         OnStop.BeginInvoke(sender, data, null, null);
     }
 }
Esempio n. 2
0
 internal void RaiseOnStop(string sender = null, OnStopdataType data = null)
 {
     if (OnStop != null)
     {
         OnStop.BeginInvoke(sender, data, null, null);
     }
 }
        /// <summary>
        /// 终止所有线程,清空待执行任务
        /// </summary>
        public void Stop()
        {
            switchTimer();

            //这里应该直接压入到已完成队列,而不是清掉
            while (AllTaskCount > 0)
            {
                T t;
                if (AllTasks.TryTake(out t))
                {
                    if (DoneTasks.TryAdd(t.Id, t) && DoneTasksAdded != null)
                    {
                        DoneTasksAdded.Invoke(this, new TaskArgs(t, this, t.AsyncState));
                    }
                }
            }

            Task.Factory.StartNew(() =>
            {
                while (WorkTaskCount > 0)
                {
                    //给正在执行的线程传入CancelToken
                    Parallel.ForEach(WorkTasks, (x, y) =>
                    {
                        if (x.Value.Status == TaskStatus.Canceled ||
                            x.Value.Status == TaskStatus.Faulted ||
                            x.Value.Status == TaskStatus.RanToCompletion)
                        {
                            DoneTasks.TryAdd(x.Key, x.Value);
                            if (DoneTasksAdded != null)
                            {
                                DoneTasksAdded.Invoke(this, new TaskArgs(x.Value, this, x.Value.AsyncState));
                            }
                            T t;
                            WorkTasks.TryRemove(x.Key, out t);
                        }
                        else
                        {
                            x.Value.Cancel();
                        }
                    });

                    if (OnStoping != null)
                    {
                        OnStoping.BeginInvoke(this, null, null, null);
                    }
                    System.Threading.Thread.Sleep(10);
                }
            }).ContinueWith((x) =>
            {
                if (OnStop != null)
                {
                    OnStop.BeginInvoke(this, null, null, null);
                }
                _isStoped = true;
                switchTimer();
            });
        }