📄 tasklist.cs
字号:
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using TaskSchedulerInterop;
namespace TaskScheduler
{
/// <remarks>
/// Provides acccess to the tasks of the system.
/// </remarks>
public class TaskList : IEnumerable, IDisposable
{
/// <summary>
/// Underlying COM interface
/// </summary>
private ITaskScheduler its = null;
/// <summary>
/// Internal constructor - no public creation of TaskList objects, they are created from the Scheduler.
/// </summary>
internal TaskList()
{
CTaskScheduler cts = new CTaskScheduler();
its = (ITaskScheduler)cts;
}
/// <summary>
/// Enumerator for <c>TaskList</c>
/// </summary>
private class Enumerator : IEnumerator
{
private IEnumWorkItems wienum = null;
private ITaskScheduler m_ts = null;
private ITask m_iTaskCurrent = null;
private string m_taskName = null;
/// <summary>
/// Internal constructor - Only accessable through <see cref="Scheduler.Tasks.GetEnumerator()"/>
/// </summary>
/// <param name="ts">ITaskScheduler instance</param>
internal Enumerator(ITaskScheduler ts)
{
m_ts = ts;
m_ts.Enum(out wienum);
Reset();
}
/// <summary>
/// Gets the name of the next task
/// </summary>
/// <returns>Name of next task</returns>
private string GetNextName()
{
System.IntPtr ptr = IntPtr.Zero;
string ret = null;
uint uFetched = 0;
try
{
wienum.Next(1, out ptr, out uFetched);
}
catch
{
}
finally
{
if (uFetched == 1)
{
ret = Marshal.PtrToStringAuto(Marshal.ReadIntPtr(ptr));
Marshal.FreeCoTaskMem((IntPtr)Marshal.ReadInt32(ptr));
}
Marshal.FreeCoTaskMem(ptr);
}
return ret;
}
/// <summary>
/// Moves to the next task. See <see cref="IEnumerator.MoveNext()"/> for more information.
/// </summary>
/// <returns>true if next task found, false if no more tasks.</returns>
public bool MoveNext()
{
string curItem = GetNextName();
if (curItem == null)
{
m_iTaskCurrent = null;
m_taskName = null;
return false;
}
object o;
m_ts.Activate(curItem, ref ITaskGuid, out o);
m_iTaskCurrent = (ITask)o;
m_taskName = curItem;
return true;
}
/// <summary>
/// Reset task enumeration. See <see cref="IEnumerator.Reset()"/> for more information.
/// </summary>
public void Reset()
{
m_iTaskCurrent = null;
m_taskName = null;
wienum.Reset();
}
/// <summary>
/// Retrieves the current task. See <see cref="IEnumerator.Current"/> for more information.
/// </summary>
public object Current
{
get
{
return new Task(m_iTaskCurrent, m_taskName);
}
}
}
/// <summary>
/// Name of target computer
/// </summary>
internal string TargetComputer
{
get
{
string ret;
its.GetTargetComputer(out ret);
return ret;
}
set
{
if (!value.StartsWith("\\\\"))
its.SetTargetComputer("\\\\" + value);
else
its.SetTargetComputer(value);
}
}
/// <summary>
/// Creates a new task on the system with the supplied <paramref name="name" />.
/// </summary>
/// <param name="name">Unique display name for the task. If not unique, an ArgumentException will be thrown.</param>
/// <returns>Instance of new task</returns>
/// <exception cref="ArgumentException">There is already a task of the same name as the one supplied for the new task.</exception>
public Task NewTask(string name)
{
if (this[name] != null)
throw new ArgumentException("The task named \"" + name + "\" already exists.");
try
{
object o;
its.NewWorkItem(name, ref CTaskGuid, ref ITaskGuid, out o);
ITask iTask = (ITask)o;
return new Task(iTask, name);
}
catch
{
return null;
}
}
/// <summary>
/// Deletes the task of the given <paramref name="name" />.
/// </summary>
/// <param name="name">Name of task to delete</param>
public void Delete(string name)
{
its.Delete(name);
}
/// <summary>
/// Indexer which retrieves task of given <paramref name="name" />.
/// </summary>
/// <param name="name">Name of task to retrieve</param>
public Task this[string name]
{
get
{
try
{
object o;
its.Activate(name, ref ITaskGuid, out o);
ITask iTask = (ITask)o;
return new Task(iTask, name);
}
catch
{
return null;
}
}
}
#region Implementation of IEnumerable
/// <summary>
/// Gets a TaskList enumerator
/// </summary>
/// <returns>Enumerator for TaskList</returns>
public System.Collections.IEnumerator GetEnumerator()
{
return new Enumerator(its);
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Disposes object
/// </summary>
public void Dispose()
{
its = null;
}
#endregion
internal static Guid ITaskGuid;
internal static Guid CTaskGuid;
static TaskList()
{
ITaskGuid = Marshal.GenerateGuidForType(typeof(ITask));
CTaskGuid = Marshal.GenerateGuidForType(typeof(CTask));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -