Example #1
0
 public void queueTask(CustomThreadTask task)
 {
     lock (_lockQueue)
     {
         _queueTasks.Enqueue(task);
         debug("Task Added: " + task.name);
     }
 }
Example #2
0
        void startTask(int index, CustomThreadTask task)
        {
            _customThreadPool[index] = new Thread(
                new ThreadStart(task.run));

            _customThreadPool[index].Name = task.ToString();

            _customThreadPool[index].Start();
        }
Example #3
0
        bool getTaskAvailable(ref CustomThreadTask task)
        {
            lock (_lockQueue)
            {
                if (_queueTasks.Count > 0)
                {
                    task = _queueTasks.Dequeue();
                }
            }

            return(null != task);
        }
Example #4
0
        void mainThreadFunction()
        {
            try
            {
                debug("Main thread start");

                while (_running)
                {
                    int index = 0;

                    if (getThreadAvailable(ref index))
                    {
                        CustomThreadTask task = null;

                        if (getTaskAvailable(ref task))
                        {
                            startTask(index, task);
                        }
                    }

                    Thread.Sleep(1);
                }
            }
            catch (Exception ex)
            {
                debug("Main thread exception: " + ex.Message);
            }
            finally
            {
                waitForAll();

                lock (_lockQueue)
                    _queueTasks.Clear();

                for (int k = 0; k < _customThreadPool.Length; k++)
                {
                    _customThreadPool[k] = null;
                }

                debug("Main thread stop");
            }
        }