示例#1
0
        /// <summary>Empties the work queue of any queued work items.</summary>
        public void EmptyQueue()
        {
            lock (workerQueue)
            {
                try
                {
                    // Try to dispose of all remaining state
                    foreach (object obj in workerQueue)
                    {
                        WorkerCallback callback = (WorkerCallback)obj;
                        if (callback.State is IDisposable)
                        {
                            ((IDisposable)callback.State).Dispose();
                        }
                    }
                }
                catch
                {
                    // Make sure an error isn't thrown.
                }

                // Clear all waiting items and reset the number of worker threads currently needed
                // to be 0 (there is nothing for threads to do)
                workerQueue.Clear();
            }
        }
示例#2
0
        /// <summary>A thread worker function that processes items from the work queue.</summary>
        private void ProcessQueuedItems()
        {
            // Process indefinitely
            while (true)
            {
                // Get the next item in the queue.  If there is nothing there, go to sleep
                // for a while until we're woken up when a callback is waiting.
                WorkerCallback callback = null;
                while (callback == null)
                {
                    // Try to get the next callback available.  We need to lock on the
                    // queue in order to make our count check and retrieval atomic.
                    lock (workerQueue)
                    {
                        if (workerQueue.Count > 0)
                        {
                            try
                            {
                                callback = (WorkerCallback)workerQueue.Dequeue();
                            }
                            catch {}                            // make sure not to fail here
                        }
                    }

                    // If we can't get one, go to sleep.
                    if (callback == null)
                    {
                        if (stopThreads)
                        {
                            //Return from the method which will in turn stop the thread
                            return;
                        }

                        lock (waitCount)
                        {
                            waitCount.Value++;
                        }
                        //Wait until there is more work to be done;
                        workerThreadsSemaphore.WaitOne();
                    }
                }

                // We now have a callback.  Execute it.  Make sure to accurately
                // record how many callbacks are currently executing.
                try
                {
                    callback.Callback(callback.State);
                }
                catch
                {
                    // Make sure we don't throw here.  Errors are not our problem.
                }
            }
        }
示例#3
0
 private static void StartWorkerService()
 {
     Task.Factory.StartNew(() =>
     {
         var proc            = StartWorkerProcess();
         var pipeName        = string.Format(PIPE_NAME_FORMAT, proc.Id);
         var callback        = new WorkerCallback();
         var instanceContext = new InstanceContext(callback);
         var channelFactory  = new DuplexChannelFactory <IWorkerService>(instanceContext,
                                                                         new NetNamedPipeBinding
         {
             SendTimeout            = TimeSpan.FromHours(24),
             MaxReceivedMessageSize = int.MaxValue
         },
                                                                         new EndpointAddress(pipeName));
         var channel = channelFactory.CreateChannel();
         _workerQueue.Add(new WorkerContext {
             Service = channel, Callback = callback, Process = proc
         });
     });
 }
示例#4
0
        /// <summary>Queues a user work item to the thread pool.</summary>
        /// <param name="callback">
        /// A WaitCallback representing the delegate to invoke when the thread in the
        /// thread pool picks up the work item.
        /// </param>
        /// <param name="state">
        /// The object that is passed to the delegate when serviced from the thread pool.
        /// </param>
        public void QueueUserWorkItem(WaitCallback callback, object state)
        {
            // Create a waiting callback that contains the delegate and its state.
            // Add it to the processing queue, and signal that data is waiting.
            WorkerCallback waiting = new WorkerCallback(callback, state);

            lock (workerQueue)
            {
                workerQueue.Enqueue(waiting);
            }

            //Interlocked does not let us read the value of an int, so instead lets
            //add 0 to the value to get the actual value of waitingThreads
            lock (waitCount)
            {
                if (waitCount.Value > 0)
                {
                    //Only call release if we still have waiting threads, otherwise an exception will be
                    //thrown by the semaphore.
                    waitCount.Value--;
                    workerThreadsSemaphore.Release(1);
                }
            }
        }
示例#5
0
 public RestoreThemeListsWorker(TmTreeNode treeNode, WorkerCallback func)
 {
     _node = treeNode;
     _func = func;
 }
示例#6
0
 public MDLCallback(WorkerCallback callback, T[] objects_array, uint size)
 {
     objects      = objects_array;
     _callback    = callback;
     objects_size = size;
 }
 public bool ExecuteUnmanaged()
 {
     Callback = new WorkerCallback(WorkerCallbackHandler);
     return Execute(Callback, "Pass an String");
 }
 public static extern bool Execute(WorkerCallback callback, string name);
示例#9
0
        /// <summary>Queues a user work item to the thread pool.</summary>
        /// <param name="callback">
        /// A WaitCallback representing the delegate to invoke when the thread in the 
        /// thread pool picks up the work item.
        /// </param>
        /// <param name="state">
        /// The object that is passed to the delegate when serviced from the thread pool.
        /// </param>
        public void QueueUserWorkItem(WaitCallback callback, object state)
        {
            // Create a waiting callback that contains the delegate and its state.
            // Add it to the processing queue, and signal that data is waiting.
            WorkerCallback waiting = new WorkerCallback(callback, state);
            lock (workerQueue)
            {
                workerQueue.Enqueue(waiting);
            }

            //Interlocked does not let us read the value of an int, so instead lets
            //add 0 to the value to get the actual value of waitingThreads
            lock (waitCount)
            {
                if (waitCount.Value > 0)
                {
                    //Only call release if we still have waiting threads, otherwise an exception will be
                    //thrown by the semaphore.
                    waitCount.Value--;
                    workerThreadsSemaphore.Release(1);
                }
            }
        }