📄 triggerlist.cs
字号:
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using TaskSchedulerInterop;
namespace TaskScheduler
{
/// <summary>
/// TriggerList encapsulates the list of triggers associated with as task.
/// </summary>
public class TriggerList : IList, IDisposable
{
/// <summary>
/// Internal COM interface
/// </summary>
private ITask iTask;
/// <summary>
/// Internal constructor creates trigger collection
/// </summary>
/// <param name="iTask">Instance of an ITask</param>
internal TriggerList(ITask iTask)
{
this.iTask = iTask;
}
/// <summary>
/// Enumerator for TriggerList
/// </summary>
private class Enumerator : IEnumerator
{
private ITask iTask;
private ITaskTrigger iTrigger;
private ushort m_curItem;
/// <summary>
/// Internal constructor - Only accessable through <see cref="Task.Triggers.GetEnumerator()"/>
/// </summary>
/// <param name="iTask">Instance of an ITask</param>
internal Enumerator(ITask iTask)
{
this.iTask = iTask;
Reset();
}
/// <summary>
/// Moves to the next trigger. See <see cref="IEnumerator.MoveNext()"/> for more information.
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
ushort count;
iTask.GetTriggerCount(out count);
m_curItem++;
if (m_curItem > count)
{
iTrigger = null;
return false;
}
iTask.GetTrigger((ushort)(m_curItem - 1), out iTrigger);
return true;
}
/// <summary>
/// Reset trigger enumeration. See <see cref="IEnumerator.Reset()"/> for more information.
/// </summary>
public void Reset()
{
m_curItem = 0;
iTrigger = null;
}
/// <summary>
/// Retrieves the current trigger. See <see cref="IEnumerator.Current"/> for more information.
/// </summary>
public object Current
{
get { return Trigger.GetTypedTrigger(iTrigger); }
}
}
#region Implementation of IList
/// <summary>
/// Removes trigger at specified index
/// </summary>
/// <param name="index">Index of trigger to remove</param>
public void RemoveAt(int index)
{
if (index >= Count)
throw new ArgumentOutOfRangeException("index", index, "Failed to remove Trigger. Index out of range.");
iTask.DeleteTrigger((ushort)index);
}
/// <summary>
/// Inserts a trigger at the specified index. Not implemented.
/// </summary>
/// <param name="index">Index to insert trigger</param>
/// <param name="value">Value of trigger to insert</param>
void IList.Insert(int index, object value)
{
throw new NotImplementedException("A Trigger cannot be inserted. The ordering of Triggers is arbitrary.");
}
/// <summary>
/// Removes the matching trigger
/// </summary>
/// <param name="trigger">Trigger to remove</param>
public void Remove(Trigger trigger)
{
int i = IndexOf(trigger);
if (i != -1)
RemoveAt(i);
}
/// <summary>
/// IList.Remove implementation
/// </summary>
void IList.Remove(object value)
{
Remove(value as Trigger);
}
/// <summary>
/// Test to see if trigger is part of the collection
/// </summary>
/// <param name="trigger">Trigger to find</param>
/// <returns>true if trigger found in collection</returns>
public bool Contains(Trigger trigger)
{
return (IndexOf(trigger) != -1);
}
/// <summary>
/// IList.Contains implementation
/// </summary>
bool IList.Contains(object value)
{
return Contains(value as Trigger);
}
/// <summary>
/// Remove all triggers from collection
/// </summary>
public void Clear()
{
for (int i = Count-1; i >= 0; i--)
{
RemoveAt(i);
}
}
/// <summary>
/// Returns the index of the supplied Trigger
/// </summary>
/// <param name="trigger">Trigger to find</param>
/// <returns>Zero based index of value, -1 if not found</returns>
public int IndexOf(Trigger trigger)
{
for (int i = 0; i < Count; i++)
{
if (this[i].Equals(trigger))
return i;
}
return -1;
}
/// <summary>
/// IList.IndexOf implementation
/// </summary>
int IList.IndexOf(object value)
{
return IndexOf(value as Trigger);
}
/// <summary>
/// Add the supplied trigger to the collection
/// </summary>
/// <param name="trigger">Trigger to add</param>
/// <returns>Index of added trigger</returns>
public int Add(Trigger trigger)
{
ITaskTrigger iTrigger;
ushort index;
iTask.CreateTrigger(out index, out iTrigger);
iTrigger.SetTrigger(ref trigger.taskTrigger);
return (int)index;
}
/// <summary>
/// IList.Add implementation
/// </summary>
int IList.Add(object value)
{
return Add(value as Trigger);
}
/// <summary>
/// Gets read-only state of collection. Always false.
/// </summary>
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Gets/sets the trigger at the specified index
/// </summary>
public Trigger this[int index]
{
get
{
if (index >= Count)
throw new ArgumentOutOfRangeException("index", index, "Failed to remove Trigger. Index out of range.");
ITaskTrigger iTrigger;
iTask.GetTrigger((ushort)index, out iTrigger);
return Trigger.GetTypedTrigger(iTrigger);
}
set
{
if (index >= Count)
throw new ArgumentOutOfRangeException("index", index, "Failed to remove Trigger. Index out of range.");
ITaskTrigger iTrigger;
iTask.GetTrigger((ushort)index, out iTrigger);
iTrigger.SetTrigger(ref value.taskTrigger);
}
}
/// <summary>
/// IList.this[int] implementation
/// </summary>
object IList.this[int index]
{
get { return this[index]; }
set { this[index] = (value as Trigger); }
}
/// <summary>
/// Returns whether collection is a fixed size. Always returns false.
/// </summary>
public bool IsFixedSize
{
get { return false; }
}
#endregion
#region Implementation of ICollection
/// <summary>
/// Copies list of triggers to supplied array beginning at index
/// </summary>
/// <param name="array">Array to copy triggers into</param>
/// <param name="index">Index at which to start inserting</param>
public void CopyTo(System.Array array, int index)
{
if (array == null)
throw new ArgumentNullException("array", "Destination array cannot be null");
if (index + Count > array.GetUpperBound(0))
throw new ArgumentException("The number of Triggers is greater than the available space from index to the end of the destination array.", "array");
for (int i = 0; i < Count; i++)
{
array.SetValue(this[i], i+index);
}
}
/// <summary>
/// Returns synchronizable state. Always false since the Task Scheduler is not
/// thread safe.
/// </summary>
public bool IsSynchronized
{
get { return false; }
}
/// <summary>
/// Gets the number of triggers in the collection
/// </summary>
public int Count
{
get
{
ushort cnt = 0;
iTask.GetTriggerCount(out cnt);
return (int)cnt;
}
}
/// <summary>
/// Gets the root object for synchronization. Always null.
/// </summary>
public object SyncRoot
{
get { return null; }
}
#endregion
#region Implementation of IEnumerable
/// <summary>
/// Gets a TriggerList enumerator
/// </summary>
/// <returns>Enumerator for TriggerList</returns>
public System.Collections.IEnumerator GetEnumerator()
{
return new Enumerator(iTask);
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Disposes object
/// </summary>
public void Dispose()
{
iTask = null;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -