Exemple #1
0
        protected virtual void NotifyStart()
        {
            if (State == TaskState.Running)
            {
                if (OnTaskStart != null)
                {
                    OnTaskStart.Invoke(this);
                }

                StartWasNotified = true;
            }
        }
        // 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);
        }
        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();
        }