📄 scheduler.cs
字号:
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that removes
//an item from the collection of schedule items in
//progress. It first obtains a write lock
//on the ScheduleInProgress object.
///''''''''''''''''''''''''''''''''''''''''''''''''
private static void RemoveFromScheduleInProgress (ScheduleItem scheduleItem)
{
try
{
inProgressReadWriteLock.AcquireWriterLock(writeTimeout);
try
{
// It is safe for this thread to read or write
// from the shared resource.
scheduleInProgress.Remove(scheduleItem);//.ScheduleID.ToString());
Interlocked.Increment(ref writes);
}
catch (System.ArgumentException)
{
}
finally
{
// Ensure that the lock is released.
inProgressReadWriteLock.ReleaseWriterLock();
}
}
catch (ApplicationException exception)
{
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
Exceptions.Exceptions.LogException(exception);
}
}
private static bool ScheduleQueueContains(ScheduleItem scheduleItem)
{
foreach(ScheduleItem scheduleItem2 in ScheduleQueue)
{
if (scheduleItem2.ScheduleID == scheduleItem.ScheduleID)
{
return true;
}
}
return false;
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that adds
//an item to the collection of schedule items in
//queue. It first obtains a write lock
//on the ScheduleQueue object.
///''''''''''''''''''''''''''''''''''''''''''''''''
public static void AddToScheduleQueue (ScheduleHistoryItem scheduleHistoryItem)
{
if (! ScheduleQueueContains(scheduleHistoryItem))
{
try
{
queueReadWriteLock.AcquireWriterLock(writeTimeout);
try
{
// It is safe for this thread to read or write
// from the shared resource.
ScheduleQueue.Add(scheduleHistoryItem);//, scheduleHistoryItem.ScheduleID.ToString());
Interlocked.Increment(ref writes);
}
finally
{
// Ensure that the lock is released.
queueReadWriteLock.ReleaseWriterLock();
}
}
catch (ApplicationException exception)
{
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
Exceptions.Exceptions.LogException(exception);
}
}
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that clears
//the collection of schedule items in
//queue. It first obtains a write lock
//on the ScheduleQueue object.
///''''''''''''''''''''''''''''''''''''''''''''''''
private static void ClearScheduleQueue ()
{
try
{
int intCount = GetScheduleQueueCount();
if (intCount == 0)
{
return;
}
queueReadWriteLock.AcquireWriterLock(writeTimeout);
try
{
int i;
for (i = 0; i < intCount; i++)
{
ScheduleQueue.RemoveAt(i);
}
Interlocked.Increment(ref writes);
}
finally
{
// Ensure that the lock is released.
queueReadWriteLock.ReleaseWriterLock();
}
}
catch (ApplicationException exception)
{
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
Exceptions.Exceptions.LogException(exception);
}
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that removes
//an item from the collection of schedule items in
//queue. It first obtains a write lock
//on the ScheduleQueue object.
///''''''''''''''''''''''''''''''''''''''''''''''''
public static void RemoveFromScheduleQueue (ScheduleItem scheduleItem)
{
try
{
queueReadWriteLock.AcquireWriterLock(writeTimeout);
try
{
// It is safe for this thread to read or write
// from the shared resource.
ScheduleQueue.Remove(scheduleItem.ScheduleID.ToString());
Interlocked.Increment(ref writes);
}
catch (System.ArgumentException)
{
}
finally
{
// Ensure that the lock is released.
queueReadWriteLock.ReleaseWriterLock();
}
}
catch (ApplicationException exception)
{
// The writer lock request timed out.
Interlocked.Increment(ref writerTimeouts);
Exceptions.Exceptions.LogException(exception);
}
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that returns
//the number of items in the collection of
//schedule items in queue. It first obtains a
//read lock on the ScheduleQueue object.
///''''''''''''''''''''''''''''''''''''''''''''''''
static public ArrayList GetScheduleQueue()
{
ArrayList list = null;
try
{
queueReadWriteLock.AcquireReaderLock(readTimeout);
try
{
// It is safe for this thread to read from
// the shared resource.
list = ScheduleQueue;
Interlocked.Increment(ref reads);
}
finally
{
// Ensure that the lock is released.
queueReadWriteLock.ReleaseReaderLock();
}
}
catch (ApplicationException)
{
// The reader lock request timed out.
Interlocked.Increment(ref readerTimeouts);
}
return list;
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that returns
//the number of items in the collection of
//schedule items in progress. It first obtains a
//read lock on the ScheduleProgress object.
///''''''''''''''''''''''''''''''''''''''''''''''''
static public ArrayList GetScheduleInProgress()
{
ArrayList list = null;
try
{
inProgressReadWriteLock.AcquireReaderLock(readTimeout);
try
{
// It is safe for this thread to read from
// the shared resource.
list = ScheduleInProgress;
Interlocked.Increment(ref reads);
}
finally
{
// Ensure that the lock is released.
inProgressReadWriteLock.ReleaseReaderLock();
}
}
catch (ApplicationException)
{
// The reader lock request timed out.
Interlocked.Increment(ref readerTimeouts);
}
return list;
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that returns
//the number of items in the collection of
//schedule items in queue. It first obtains a
//read lock on the ScheduleQueue object.
///''''''''''''''''''''''''''''''''''''''''''''''''
static public int GetScheduleQueueCount()
{
int intCount = 0;
try
{
queueReadWriteLock.AcquireReaderLock(readTimeout);
try
{
// It is safe for this thread to read from
// the shared resource.
intCount = ScheduleQueue.Count;
Interlocked.Increment(ref reads);
}
finally
{
// Ensure that the lock is released.
queueReadWriteLock.ReleaseReaderLock();
}
}
catch (ApplicationException)
{
// The reader lock request timed out.
Interlocked.Increment(ref readerTimeouts);
}
return intCount;
}
///''''''''''''''''''''''''''''''''''''''''''''''''
//This is a multi-thread safe method that returns
//the number of items in the collection of
//schedule items in progress. It first obtains a
//read lock on the ScheduleProgress object.
///''''''''''''''''''''''''''''''''''''''''''''''''
static public int GetScheduleInProgressCount()
{
int intCount = 0;
try
{
inProgressReadWriteLock.AcquireReaderLock(readTimeout);
try
{
// It is safe for this thread to read from
// the shared resource.
intCount = ScheduleInProgress.Count;
Interlocked.Increment(ref reads);
}
finally
{
// Ensure that the lock is released.
inProgressReadWriteLock.ReleaseReaderLock();
}
}
catch (ApplicationException)
{
// The reader lock request timed out.
Interlocked.Increment(ref readerTimeouts);
}
return intCount;
}
static public void WorkStarted (SchedulerClient schedulerClient)
{
bool activeThreadCountIncremented = false;
try
{
schedulerClient.ScheduleHistoryItem.ThreadID = Thread.CurrentThread.GetHashCode();
///''''''''''''''''''''''''''''''''''''''''''''''''
//Put the object in the ScheduleInProgress collection
//and remove it from the ScheduleQueue
///''''''''''''''''''''''''''''''''''''''''''''''''
RemoveFromScheduleQueue(schedulerClient.ScheduleHistoryItem);
AddToScheduleInProgress(schedulerClient.ScheduleHistoryItem);
///''''''''''''''''''''''''''''''''''''''''''''''''
//A SchedulerClient is notifying us that their
//process has started. Increase our ActiveThreadCount
///''''''''''''''''''''''''''''''''''''''''''''''''
Interlocked.Increment(ref activeThreadCount);
activeThreadCountIncremented = true;
///''''''''''''''''''''''''''''''''''''''''''''''''
//Update the schedule item
//object property to note the start time.
///''''''''''''''''''''''''''''''''''''''''''''''''
schedulerClient.ScheduleHistoryItem.StartDate = DateTime.Now;
CoreScheduler.AddScheduleHistory(schedulerClient.ScheduleHistoryItem);
if (schedulerClient.ScheduleHistoryItem.RetainHistoryNum > 0)
{
///''''''''''''''''''''''''''''''''''''''''''''''''
//Write out the log entry for this event
///''''''''''''''''''''''''''''''''''''''''''''''''
EventLogController eventLog = new EventLogController();
LogInfo eventLogInfo = new LogInfo();
eventLogInfo.AddProperty("THREAD ID", Thread.CurrentThread.GetHashCode().ToString());
eventLogInfo.AddProperty("TYPE", schedulerClient.GetType().FullName);
eventLogInfo.AddProperty("SOURCE", schedulerClient.ScheduleHistoryItem.ScheduleSource.ToString());
eventLogInfo.AddProperty("ACTIVE THREADS", activeThreadCount.ToString());
eventLogInfo.AddProperty("FREE THREADS", FreeThreads.ToString());
eventLogInfo.AddProperty("READER TIMEOUTS", readerTimeouts.ToString());
eventLogInfo.AddProperty("WRITER TIMEOUTS", writerTimeouts.ToString());
eventLogInfo.AddProperty("IN PROGRESS", GetScheduleInProgressCount().ToString());
eventLogInfo.AddProperty("IN QUEUE", GetScheduleQueueCount().ToString());
eventLogInfo.LogTypeKey = "SCHEDULER_EVENT_STARTED";
eventLog.AddLog(eventLogInfo);
}
}
catch (Exception exception)
{
//Decrement the ActiveThreadCount because
//otherwise the number of active threads
//will appear to be climbing when in fact
//no tasks are being executed.
if (activeThreadCountIncremented)
{
Interlocked.Decrement(ref activeThreadCount);
}
Exceptions.Exceptions.ProcessSchedulerException(exception);
}
}
static public void WorkProgressing (SchedulerClient schedulerClient)
{
try
{
///''''''''''''''''''''''''''''''''''''''''''''''''
//A SchedulerClient is notifying us that their
//process is in progress. Informational only.
///''''''''''''''''''''''''''''''''''''''''''''''''
if (schedulerClient.ScheduleHistoryItem.RetainHistoryNum > 0)
{
///''''''''''''''''''''''''''''''''''''''''''''''''
//Write out the log entry for this event
///''''''''''''''''''''''''''''''''''''''''''''''''
EventLogController eventLog = new EventLogController();
LogInfo eventLogInfo = new LogInfo();
eventLogInfo.AddProperty("THREAD ID", Thread.CurrentThread.GetHashCode().ToString());
eventLogInfo.AddProperty("TYPE", schedulerClient.GetType().FullName);
eventLogInfo.AddProperty("SOURCE", schedulerClient.ScheduleHistoryItem.ScheduleSource.ToString());
eventLogInfo.AddProperty("ACTIVE THREADS", activeThreadCount.ToString());
eventLogInfo.AddProperty("FREE THREADS", FreeThreads.ToString());
eventLogInfo.AddProperty("READER TIMEOUTS", readerTimeouts.ToString());
eventLogInfo.AddProperty("WRITER TIMEOUTS", writerTimeouts.ToString());
eventLogInfo.AddProperty("IN PROGRESS", GetScheduleInProgressCount().ToString());
eventLogInfo.AddProperty("IN QUEUE", GetScheduleQueueCount().ToString());
eventLogInfo.LogTypeKey = "SCHEDULER_EVENT_PROGRESSING";
eventLog.AddLog(eventLogInfo);
}
}
catch (Exception exception)
{
Exceptions.Exceptions.ProcessSchedulerException(exception);
}
}
static public void WorkCompleted (SchedulerClient schedulerClient)
{
try
{
ScheduleHistoryItem scheduleHistoryItem;
scheduleHistoryItem = schedulerClient.ScheduleHistoryItem;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -