Exemplo n.º 1
0
        public bool AddQueuedThreadSync(QueuedThread <T> .QueuedThreadTarget Target, T Parameter)
        {
            try
            {
                if (_queue.Count >= _queueLength)
                {
                    return(false);
                }

                ManualResetEvent resetEvent = new ManualResetEvent(false);
                QueuedThread <T> thread     = new QueuedThread <T>(Target, Parameter, this.QueuedThreadFinshed, resetEvent);
                lock (lockThis)
                {
                    _queue.Enqueue(thread);
                }

                if (_currentThreads < _maxThreads)
                {
                    TryStartThread();
                }

                resetEvent.WaitOne();

                return(true);
            }
            catch (Exception ex)
            {
                Logger.Log(loggingMethod.error, "ThreadQueue.AddQueuedThreadSync: " + ex.Message);
                return(false);
            }
        }
Exemplo n.º 2
0
        private void TryStartThread()
        {
            try
            {
                lock (lockThis)
                {
                    if (_currentThreads >= _maxThreads)
                    {
                        return;
                    }
                    int count = Math.Min(_maxThreads - _currentThreads, _queue.Count);

                    for (int i = 0; i < count; i++)
                    {
                        QueuedThread <T> queuedThread = _queue.Dequeue();
                        try
                        {
                            if (queuedThread == null)
                            {
                                throw new ArgumentException("Thread Object in Queue is NULL");
                            }

                            // ThreadPool Way
                            _currentThreads++;
                            ThreadPool.QueueUserWorkItem(queuedThread.Start);

                            // Thread Way: mehr overhead
                            //Thread thread = new Thread(new ThreadStart(queuedThread.Start));
                            //_currentThreads++;
                            //thread.Start();
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(loggingMethod.error,
                                       "Starting Thread Error: " + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace);

                            if (queuedThread != null)
                            {
                                queuedThread.Dispose();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(loggingMethod.error, "ThreadQueue.TryStartThread: " + ex.Message);
            }
        }
Exemplo n.º 3
0
 public void QueuedThreadFinshed(QueuedThread <T> sender)
 {
     try
     {
         if (_currentThreads > 0)  // ist 0, wenn zwischendurch Clear durgeführt wird
         {
             _currentThreads--;
             if (_currentThreads == 0)
             {
                 // wenn alle fertig sind können neue gestartet werden...
                 // (wenn jedesmal aufgerufen wird, wird ein schon lage gestarteteter
                 // immer wieder blokiert
                 // so werden alte einmal fertig gemacht
                 TryStartThread();
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Log(loggingMethod.error, "ThreadQueue.QueuedThreadFinshed: " + ex.Message);
     }
 }