示例#1
0
 /// <summary>
 ///     Creates a thread manager which provides starting actions in new threads.
 /// </summary>
 /// <param name="parallelThreads">The maximum number of threads that could be efficiently run in parallel.</param>
 protected ThreadManager(byte parallelThreads)
 {
     _parallelThreads = parallelThreads;
     _computationalThreadStatuses = new ComputationalThreadStatus[parallelThreads];
     _threadStatuses = Array.AsReadOnly(_computationalThreadStatuses);
     _availableThreads = new ConcurrentQueue<int>();
     for (var i = 0; i < _computationalThreadStatuses.Length; i++)
     {
         _computationalThreadStatuses[i] = new ComputationalThreadStatus();
         _availableThreads.Enqueue(i);
     }
 }
示例#2
0
 /// <summary>
 ///     Creates a thread manager which provides starting actions in new threads.
 /// </summary>
 /// <param name="parallelThreads">The maximum number of threads that could be efficiently run in parallel.</param>
 protected ThreadManager(byte parallelThreads)
 {
     _parallelThreads             = parallelThreads;
     _computationalThreadStatuses = new ComputationalThreadStatus[parallelThreads];
     _threadStatuses   = Array.AsReadOnly(_computationalThreadStatuses);
     _availableThreads = new ConcurrentQueue <int>();
     for (var i = 0; i < _computationalThreadStatuses.Length; i++)
     {
         _computationalThreadStatuses[i] = new ComputationalThreadStatus();
         _availableThreads.Enqueue(i);
     }
 }
示例#3
0
        /// <summary>
        ///     Starts executing action if there is an available idle thread. This method gets information needed for Status
        ///     messages.
        /// </summary>
        /// <param name="actionToExecute">An action to be executed in new thread. If null no new thread will be started.</param>
        /// <param name="actionOnException">
        ///     An action to be executed when exception occurs in started thread. If null exception
        ///     will be ignored.
        /// </param>
        /// <param name="problemType">The name of the type as given by TaskSolver.</param>
        /// <param name="problemInstanceId">The ID of the problem assigned when client connected.</param>
        /// <param name="partialProblemId">The ID of the task within given problem instance.</param>
        /// <returns>True if thread was successfully started; false otherwise.</returns>
        public bool StartInNewThread(Action actionToExecute, Action <Exception> actionOnException, string problemType,
                                     ulong?problemInstanceId, ulong?partialProblemId)
        {
            if (actionToExecute == null)
            {
                return(false);
            }

            int threadIndex;

            if (!_availableThreads.TryDequeue(out threadIndex))
            {
                return(false);
            }

            var started = StartInNewThread(() =>
            {
                _computationalThreadStatuses[threadIndex] = new ComputationalThreadStatus(problemType, problemInstanceId,
                                                                                          partialProblemId);
                try
                {
                    actionToExecute();
                }
                catch (Exception ex)
                {
                    if (actionOnException != null)
                    {
                        actionOnException(ex);
                    }
                }
                finally
                {
                    _computationalThreadStatuses[threadIndex] = new ComputationalThreadStatus();
                    _availableThreads.Enqueue(threadIndex);
                }
            });

            return(started);
        }
示例#4
0
        /// <summary>
        ///     Starts executing action if there is an available idle thread. This method gets information needed for Status
        ///     messages.
        /// </summary>
        /// <param name="actionToExecute">An action to be executed in new thread. If null no new thread will be started.</param>
        /// <param name="actionOnException">
        ///     An action to be executed when exception occurs in started thread. If null exception
        ///     will be ignored.
        /// </param>
        /// <param name="problemType">The name of the type as given by TaskSolver.</param>
        /// <param name="problemInstanceId">The ID of the problem assigned when client connected.</param>
        /// <param name="partialProblemId">The ID of the task within given problem instance.</param>
        /// <returns>True if thread was successfully started; false otherwise.</returns>
        public bool StartInNewThread(Action actionToExecute, Action<Exception> actionOnException, string problemType,
            ulong? problemInstanceId, ulong? partialProblemId)
        {
            if (actionToExecute == null)
                return false;

            int threadIndex;
            if (!_availableThreads.TryDequeue(out threadIndex))
                return false;

            var started = StartInNewThread(() =>
            {
                _computationalThreadStatuses[threadIndex] = new ComputationalThreadStatus(problemType, problemInstanceId,
                    partialProblemId);
                try
                {
                    actionToExecute();
                }
                catch (Exception ex)
                {
                    if (actionOnException != null)
                        actionOnException(ex);
                }
                finally
                {
                    _computationalThreadStatuses[threadIndex] = new ComputationalThreadStatus();
                    _availableThreads.Enqueue(threadIndex);
                }
            });

            return started;
        }