0
我正在寫一個類,它利用USING
語句連接到服務器上的任務調度程序並「執行任務」;像啓用/禁用任務,啓動和停止等。我想改善代碼重用,也許通過使用委託,但我不知道如何去做。以下是我當前的一些代碼:如何通過使用委託來改進我的代碼重用?
private static void StopRunningTask(string taskName, string hostName)
{
Regex regex = GetRegexForFindTask(taskName);
using (TaskService ts = new TaskService(hostName))
{
foreach (Task t in ts.FindAllTasks(regex, true))
{
if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase) && t.State == TaskState.Running)
t.Stop();
}
}
private static void RunTask(string taskName, string hostName)
{
Regex regex = GetRegexForFindTask(taskName);
using (TaskService ts = new TaskService(hostName))
{
foreach (Task t in ts.FindAllTasks(regex, true))
{
if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase) && t.State == TaskState.Ready)
t.Run();
}
}
private static void ChangeTaskEnabledState(string taskName, bool enabled, string hostName)
{
Regex regex = GetRegexForFindTask(taskName);
using (TaskService ts = new TaskService(hostName))
{
foreach (Task t in ts.FindAllTasks(regex, true))
{
if (t.Name.Equals(taskName, StringComparison.OrdinalIgnoreCase))
t.Enabled = enabled;
}
}
private static Regex GetRegexForFindTask(string taskName)
{
var regex = new Regex(string.Format(@"{0}", taskName), RegexOptions.IgnoreCase);
return regex;
}
非常感謝您的幫助。 – 2013-03-10 12:39:17