Пример #1
0
        protected virtual void NotifyStart()
        {
            if (State == TaskState.Running)
            {
                if (OnTaskStart != null)
                {
                    OnTaskStart.Invoke(this);
                }

                StartWasNotified = true;
            }
        }
Пример #2
0
        // Start new task, independent from acutal task
        private void ForceStart()
        {
            // create new internal task
            var itc = new InternalTaskContainer {
                Cts = new CancellationTokenSource()
            };

            itc.Task = Task.Run(() => SpawnerFunc(itc.Cts.Token)).ContinueWith((t) => TaskEnd(this, t));
            //swap and stop old task
            var oldItc = Interlocked.Exchange(ref _TaskContainer, itc);

            Stop(oldItc, WaitTime);
            // event notifier
            OnTaskStart?.Invoke(this, EventArgs.Empty);
        }
Пример #3
0
        private void Run()
        {
            while (Running_)
            {
                if (!TaskQueue_.TryDequeue(out var task))
                {
                    Thread.Sleep(500);
                    continue;
                }

                Working_ = true;

                $"Convert input={task.InputFile} output={task.OutputFile} type={task.Type.ToString()}".Info();
                OnTaskStart.Invoke();

                try
                {
                    switch (task.Type)
                    {
                    case ConversionType.PDF:
                        Converter.ConvertToPDF(task.InputFile, task.OutputFile);
                        break;

                    case ConversionType.PNG:
                        Converter.ConvertToPNG(task.InputFile, task.OutputFile);
                        break;
                    }
                    task.Promise.SetResult(true);
                }
                catch (Exception e)
                {
                    e.Error("ConversionWorker", "Cannot convert due to");
                    task.Promise.SetResult(false);
                }
                OnTaskEnd.Invoke();

                Working_ = false;
            }
            "Conversion worker is end".Info();
        }
Пример #4
0
        private void TaskThreadHandler()
        {
            while (!bExit)
            {
                //当前没有任务
                if (taskQueue.Count == 0)
                {
                    Thread.Sleep(1000);
                    continue;
                }
                var currentTask = taskQueue.Dequeue();
                RunningTask = currentTask;
                OnTaskChanged?.InvokeAll(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, currentResponse));
                OnTaskStart?.InvokeAll(this);
                KancolleTaskResult result = null;
                if (currentTask is OrganizeTask)
                {
                    result = Organize(currentTask as OrganizeTask);
                }
                else if (currentTask is SupplyTask)
                {
                    result = Supply(currentTask as SupplyTask);
                }
                else if (currentTask is QuestTask)
                {
                    result = Quest(currentTask as QuestTask);
                }
                else if (currentTask is MissionTask)
                {
                    result = Mission(currentTask as MissionTask);
                }
                else if (currentTask is MapTask)
                {
                    result = Map(currentTask as MapTask);
                }
                else if (currentTask is BattleTask)
                {
                    result = Battle(currentTask as BattleTask);
                }
                else if (currentTask is RemodelTask)
                {
                    result = Remodel(currentTask as RemodelTask);
                }
                else if (currentTask is RepairTask)
                {
                    result = Repair(currentTask as RepairTask);
                }
                else if (currentTask is RefreshDataTask)
                {
                    result = RefreshData(currentTask as RefreshDataTask);
                }

                if (result == null)
                {
                    result = new KancolleTaskResult(currentTask, KancolleTaskResultType.Fail, $"未能处理当前类型任务【{currentTask.GetType().Name}】", UnknownTaskType);
                }
                RunningTask = null;
                LastResult  = result;
                OnTaskFinished?.Invoke(this, result);
                Thread.Sleep(1000);
            }
            MessageBox.Show("thread finish");
        }