///<summary>
        /// Messaging system that runs tasks on the logic thread.
        ///</summary>
        private void TaskDoer()
        {
            ThreadStarted?.Invoke(this, null);

            TimerCallback watchDogCallBack = new TimerCallback(WatchDogElapsed);

            if (!(WatchDogTimerInterval is null))
            {
                threadWatchDog = new Timer(watchDogCallBack, null, 0, Convert.ToInt32(WatchDogTimerInterval.Value.TotalMilliseconds));
            }

            while (!CTStopRequested.IsCancellationRequested)
            {
                try
                {
                    if (_taskCollection.IsCompleted)
                    {
                        Waiting = true;
                        _logger()?.Debug("Waiting for next action");
                    }

                    Action currentAction = _taskCollection.Take(); //** This is Thread Blocking
                    Waiting = false;

                    currentAction.Invoke();
                    _logger()?.Verbose("General Invoke Done");

                    if (_taskCollection.Count > 2)
                    {
                        _logger()?.Warning("Task Queue count has exceeded 2. The count is now at {count}", _taskCollection.Count);
                    }
                }
                catch (Exception ex)
                {
                    if (UnhandledException != null)
                    {
                        _logger()?.Error(ex, "Unhandled Exception Last Chance Catch. {NewLine} {Message}", Environment.NewLine, ex.Message);
                        if (ex.StackTrace is null)
                        {
                            _logger()?.Error("Stack Trace was null");
                        }

                        UnhandledException(this, new UnhandledExceptionEventArgs(ex, false));
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            _logger()?.Debug("Thread loop has been closed");
            threadWatchDog.Change(Timeout.Infinite, Timeout.Infinite);
            _logger()?.Debug("Watch dog timer stop requested.");
            //** trigger ThreadClosed Event
            ThreadFinished?.Invoke(this, null);
        }
Пример #2
0
        private void OnThreadFinished(object sender, EventArgs e)
        {
            var threads = DoGetThreads().Threads;
            var previousThreads = State.GetThreads();

            var deadThreads = previousThreads.Except(threads).ToArray();
            foreach (var thread in deadThreads)
                ThreadFinished?.Invoke(this, thread.Id);
        }
Пример #3
0
        /// <typeparam name="TWorkItem">type of work item</typeparam>
        public bool DoWork <TWorkItem>(
            WorkItem <TWorkItem>[] workItems_in,
            IsReadyToWork <TWorkItem> isReadyToWork_in,
            MakeItWork <TWorkItem> makeItWork_in,
#endif

            ThreadFinished threadFinished_in
            )
        {
#if NET_1_1
            WorkItem _item;
#else
            WorkItem <TWorkItem> _item;
#endif
            bool _itemsonqueue;
            bool _othersstillworking;
            do
            {
                lock (this.WorkLocker) {
                    _item = null;
                    _itemsonqueue = false;
                    _othersstillworking = false;
                    for (int i = 0; i < workItems_in.Length; i++)
                    {
                        if (
                            (workItems_in[i].State == WorkItemState.Waiting)
                            &&
                            isReadyToWork_in(workItems_in[i].Item)                             // ask again
                            )
                        {
                            workItems_in[i].State = WorkItemState.Ready;
                        }

                        if (workItems_in[i].State == WorkItemState.Waiting)
                        {
                            _itemsonqueue = true;
                        }
                        else if (workItems_in[i].State == WorkItemState.Ready)
                        {
                            _item = workItems_in[i];
                            break;
                        }
                        else if (workItems_in[i].State == WorkItemState.Doing)
                        {
                            _othersstillworking = true;
                        }
                    }

                    if (_item != null)
                    {
                        _item.State = WorkItemState.Doing;
                    }
                    else if (!_itemsonqueue)
                    {
                        if (threadFinished_in != null)
                        {
                            threadFinished_in(_othersstillworking);
                        }

                        return(_othersstillworking);
                    }
                }

                if (_item != null)
                {
                    makeItWork_in(_item.Item);
                    _item.State = WorkItemState.Done;
                }
            } while (true);
        }
Пример #4
0
 /// <summary>
 ///     Event triggers whenever the thread is finished by itself or canceled by the user
 /// </summary>
 /// <param name="finishType"></param>
 protected virtual void OnThreadFinished(FinishType finishType)
 {
     ThreadFinished?.Invoke(this, new FinishedEventArgs(finishType));
 }
Пример #5
0
 public int ExitThread(uint ExitCode)
 {
     ThreadFinished?.Invoke(this, null);
     return(HResult.Ok);
 }