⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processgroup.cs

📁 SharpNuke源代码
💻 CS
字号:
using System;
using System.Data;
using System.Collections;
using System.Threading;
using System.Xml;

using DotNetNuke.Services.Scheduling;
using DotNetNuke.Services.Exceptions;

//
// DotNetNuke -  http://www.dotnetnuke.com
// Copyright (c) 2002-2005
// by Shaun Walker ( sales@perpetualmotion.ca ) of Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

namespace DotNetNuke.Services.Scheduling.DNNScheduling
{
	
	public class ProcessGroup
	{
		public delegate void CompletedEventHandler();

		///''''''''''''''''''''''''''''''''''''''''''''''''
		//This class represents a process group for
		//our threads to run in.
		///''''''''''''''''''''''''''''''''''''''''''''''''
		private CompletedEventHandler completedEvent;
		private static int numberOfProcessesInQueue = 0;
		private static int numberOfProcesses = 0;
		private static int processesCompleted;
		private static int ticksElapsed;
		
		static public int GetTicksElapsed
		{
			get { return ticksElapsed; }
		}
		
		static public int GetProcessesCompleted
		{
			get { return processesCompleted; }
		}
		
		static public int GetProcessesInQueue
		{
			get { return numberOfProcessesInQueue; }
		}
				
		public event CompletedEventHandler Completed
		{
			add { completedEvent += value; }
			remove { completedEvent -= value; }
		}
		
		public void Run (ScheduleHistoryItem scheduleHistoryItem)
		{
			SchedulerClient process = null;
			try
			{
				///''''''''''''''''''''''''''''''''''''''''''''''''
				//This is called from RunPooledThread()
				///''''''''''''''''''''''''''''''''''''''''''''''''
				ticksElapsed = Environment.TickCount - ticksElapsed;
				process = GetSchedulerClient(scheduleHistoryItem.TypeFullName, scheduleHistoryItem);
				
				process.ScheduleHistoryItem = scheduleHistoryItem;
				
				///''''''''''''''''''''''''''''''''''''''''''''''''
				//Set up the handlers for the CoreScheduler
				///''''''''''''''''''''''''''''''''''''''''''''''''
				process.ProcessStarted += new WorkStarted( Scheduler.CoreScheduler.WorkStarted);
				process.ProcessProgressing += new WorkProgressing( Scheduler.CoreScheduler.WorkProgressing);
				process.ProcessCompleted += new WorkCompleted( Scheduler.CoreScheduler.WorkCompleted);
				process.ProcessErrored += new WorkErrored( Scheduler.CoreScheduler.WorkErrored);
				
				///''''''''''''''''''''''''''''''''''''''''''''''''
				//This kicks off the DoWork method of the class
				//type specified in the configuration.
				///''''''''''''''''''''''''''''''''''''''''''''''''
				process.Started();
				try
				{
					process.ScheduleHistoryItem.Succeeded = false;
					process.DoWork();
				}
				catch (Exception exception)
				{
					//in case the scheduler client
					//didn't have proper exception handling
					//make sure we fire the Errored event
					process.ScheduleHistoryItem.Succeeded = false;
					process.Errored(exception);
				}
				if (process.ScheduleHistoryItem.Succeeded)
				{
					process.Completed();
				}
				///''''''''''''''''''''''''''''''''''''''''''''''''
				//If all processes in this ProcessGroup have
				//completed, set the ticksElapsed and raise
				//the Completed event.
				//I don't think this is necessary with the
				//other events.  I'll leave it for now and
				//will probably take it out later.
				///''''''''''''''''''''''''''''''''''''''''''''''''
				if (processesCompleted == numberOfProcesses)
				{
					if (processesCompleted == numberOfProcesses)
					{
						ticksElapsed = Environment.TickCount - ticksElapsed;
						if (completedEvent != null)
							completedEvent();
					}
				}
			}
			catch (Exception exception)
			{
				//in case the scheduler client
				//didn't have proper exception handling
				//make sure we fire the Errored event
				process.ScheduleHistoryItem.Succeeded = false;
				process.Errored(exception);
			}
			finally
			{
				///''''''''''''''''''''''''''''''''''''''''''''''''
				//Track how many processes have completed for
				//this instanciation of the ProcessGroup
				///''''''''''''''''''''''''''''''''''''''''''''''''
				numberOfProcessesInQueue --;
				processesCompleted ++;
			}
		}
		
		private SchedulerClient GetSchedulerClient(string process, ScheduleHistoryItem scheduleHistoryItem)
		{
			///''''''''''''''''''''''''''''''''''''''''''''''''
			//This is a method to encapsulate returning
			//an object whose class inherits SchedulerClient.
			///''''''''''''''''''''''''''''''''''''''''''''''''
			Type type = Type.GetType(process, true);
			ScheduleHistoryItem[] param = new ScheduleHistoryItem[1];
			param[0] = scheduleHistoryItem;
			
			///''''''''''''''''''''''''''''''''''''''''''''''''
			//Get the constructor for the subClass
			///''''''''''''''''''''''''''''''''''''''''''''''''
			Type[] types = new Type[1];
			types[0] = typeof(ScheduleHistoryItem);
			System.Reflection.ConstructorInfo constructor;
			constructor = type.GetConstructor(types);
			
			///''''''''''''''''''''''''''''''''''''''''''''''''
			//Return an instance of the class as an object
			///''''''''''''''''''''''''''''''''''''''''''''''''
			return ((SchedulerClient) constructor.Invoke(param));
		}
		
		///''''''''''''''''''''''''''''''''''''''''''''''''
		// This subroutine is callback for Threadpool.QueueWorkItem.  This is the necessary
		// subroutine signature for QueueWorkItem, and Run() is proper for creating a Thread
		// so the two subroutines cannot be combined, so instead just call Run from here.
		///''''''''''''''''''''''''''''''''''''''''''''''''
		private void RunPooledThread (object scheduleHistoryItem)
		{
			Run((ScheduleHistoryItem) scheduleHistoryItem);
		}
		
		///''''''''''''''''''''''''''''''''''''''''''''''''
		//Add a queue request to Threadpool with a
		//callback to RunPooledThread which calls Run()
		///''''''''''''''''''''''''''''''''''''''''''''''''
		public void AddQueueUserWorkItem (ScheduleItem scheduleItem)
		{
			numberOfProcessesInQueue ++;
			numberOfProcesses ++;
			ScheduleHistoryItem scheduleHistoryItem = new ScheduleHistoryItem();
			scheduleHistoryItem.TypeFullName = scheduleItem.TypeFullName;
			scheduleHistoryItem.ScheduleID = scheduleItem.ScheduleID;
			scheduleHistoryItem.TimeLapse = scheduleItem.TimeLapse;
			scheduleHistoryItem.TimeLapseMeasurement = scheduleItem.TimeLapseMeasurement;
			scheduleHistoryItem.RetryTimeLapse = scheduleItem.RetryTimeLapse;
			scheduleHistoryItem.RetryTimeLapseMeasurement = scheduleItem.RetryTimeLapseMeasurement;
			scheduleHistoryItem.ObjectDependencies = scheduleItem.ObjectDependencies;
			scheduleHistoryItem.CatchUpEnabled = scheduleItem.CatchUpEnabled;
			scheduleHistoryItem.Enabled = scheduleItem.Enabled;
			scheduleHistoryItem.NextStart = scheduleItem.NextStart;
			scheduleHistoryItem.ScheduleSource = scheduleItem.ScheduleSource;
			scheduleHistoryItem.ThreadID = scheduleItem.ThreadID;
			scheduleHistoryItem.ProcessGroup = scheduleItem.ProcessGroup;
			scheduleHistoryItem.RetainHistoryNum = scheduleItem.RetainHistoryNum;
			
			try
			{
				// Create a callback to subroutine RunPooledThread
				WaitCallback callback = new WaitCallback( RunPooledThread);
				// And put in a request to ThreadPool to run the process.
				ThreadPool.QueueUserWorkItem(callback,((object) scheduleHistoryItem));
				Thread.Sleep(1000);
			}
			catch (Exception exception)
			{
				Exceptions.Exceptions.ProcessSchedulerException(exception);
			}
		}
		
		public void RunSingleTask (ScheduleItem scheduleItem)
		{
			numberOfProcessesInQueue ++;
			numberOfProcesses ++;
			ScheduleHistoryItem scheduleHistoryItem = new ScheduleHistoryItem();
			scheduleHistoryItem.TypeFullName = scheduleItem.TypeFullName;
			scheduleHistoryItem.ScheduleID = scheduleItem.ScheduleID;
			scheduleHistoryItem.TimeLapse = scheduleItem.TimeLapse;
			scheduleHistoryItem.TimeLapseMeasurement = scheduleItem.TimeLapseMeasurement;
			scheduleHistoryItem.RetryTimeLapse = scheduleItem.RetryTimeLapse;
			scheduleHistoryItem.RetryTimeLapseMeasurement = scheduleItem.RetryTimeLapseMeasurement;
			scheduleHistoryItem.ObjectDependencies = scheduleItem.ObjectDependencies;
			scheduleHistoryItem.CatchUpEnabled = scheduleItem.CatchUpEnabled;
			scheduleHistoryItem.Enabled = scheduleItem.Enabled;
			scheduleHistoryItem.NextStart = scheduleItem.NextStart;
			scheduleHistoryItem.ScheduleSource = scheduleItem.ScheduleSource;
			scheduleHistoryItem.ThreadID = scheduleItem.ThreadID;
			scheduleHistoryItem.ProcessGroup = scheduleItem.ProcessGroup;
			
			try
			{
				Run(scheduleHistoryItem);
				Thread.Sleep(1000);
			}
			catch (Exception exception)
			{
				Exceptions.Exceptions.ProcessSchedulerException(exception);
			}
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -