Beispiel #1
0
 public QueueEntry(TaskType taskType)
 {
     TaskType    = taskType;
     TimeInQueue = 0.0f;
 }
Beispiel #2
0
        //-------------------------------------------------------------------------------
        //-------------------------------------------------------------------------------
        #endregion
        //-------------------------------------------------------------------------------
        // This wraps the execution of a tasks delegate in a call to allow extra functionality.
        /// <summary>
        /// Thread process for running tasks.
        /// </summary>
        /// <param name="threadInfo">upon creation.</param>
        //-------------------------------------------------------------------------------
        #region ThreadProc
        private void ThreadProcess(object threadInfo)
        {
            ThreadData threadData = threadInfo as ThreadData;

            while (true)
            {
                int        nTaskCount;
                QueueEntry queueEntry;

                m_queueLock.TryEnterUpgradeableReadLock(5);

                if (m_taskQueue.Count > 0)
                {
                    m_queueLock.EnterWriteLock();
                    queueEntry = m_taskQueue.Dequeue();
                    m_queueLock.ExitWriteLock();
                }
                else
                {
                    queueEntry = null;
                }

                m_queueLock.ExitUpgradeableReadLock();

                if (queueEntry != null)
                {
                    TaskType taskType = queueEntry.TaskType;
                    ParameterizedThreadStart taskDelegate = taskType.TaskOp;

                    Debug.Assert(taskDelegate != null);

                    threadData.NumberOfTasks++;

                    taskType.ExecutingThread = Thread.CurrentThread;

                    Console.WriteLine("processing " + taskType.Name + " on " + Thread.CurrentThread.Name);

                    taskDelegate.Invoke(taskType);

                    Console.WriteLine("finished processing " + taskType.Name + " on " + Thread.CurrentThread.Name);

                    if (taskType.Event != null && !taskType.ContinuousExecution)
                    {
                        taskType.Event.Set();
                    }
                    else
                    {
                        Type type = taskType.GetType();

                        PropertyInfo property   = type.GetProperty("ReturnData");
                        MethodInfo   methodInfo = property.GetGetMethod();


                        taskType.FireCompletedEvent(methodInfo.Invoke(taskType, null));

                        //This allows the task to be continuously executed.
                        if (taskType.ContinuousExecution)
                        {
                            ExecuteTask(taskType.Handle);
                        }
                        else
                        {
                            DestroyTask(taskType.Handle);
                        }
                    }
                }
            }
        }