public void queueTask(CustomThreadTask task) { lock (_lockQueue) { _queueTasks.Enqueue(task); debug("Task Added: " + task.name); } }
void startTask(int index, CustomThreadTask task) { _customThreadPool[index] = new Thread( new ThreadStart(task.run)); _customThreadPool[index].Name = task.ToString(); _customThreadPool[index].Start(); }
bool getTaskAvailable(ref CustomThreadTask task) { lock (_lockQueue) { if (_queueTasks.Count > 0) { task = _queueTasks.Dequeue(); } } return(null != task); }
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"); } }