本文整理汇总了C#中Microsoft.Win32.TaskScheduler.TaskService.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# TaskService.Dispose方法的具体用法?C# TaskService.Dispose怎么用?C# TaskService.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Win32.TaskScheduler.TaskService
的用法示例。
在下文中一共展示了TaskService.Dispose方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddTask
public void AddTask(int min, int hour, bool restart)
{
var taskService = new TaskService();
//Create task definition
var taskDefinition = taskService.NewTask();
taskDefinition.RegistrationInfo.Description = min + "@" + hour + "@" + ((restart) ? "r" : "s");
taskDefinition.Principal.UserId = "SYSTEM";
var trigger = new DailyTrigger
{
StartBoundary = DateTime.Today + TimeSpan.FromHours(hour) + TimeSpan.FromMinutes(min)
};
taskDefinition.Triggers.Add(trigger);
//Create task action
var fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"Power.exe");
taskDefinition.Actions.Add(restart
? new ExecAction(fileName, "reboot \"This computer is going to reboot.\"")
: new ExecAction(fileName, "shutdown \"This computer is going to shutdown to save power.\""));
taskDefinition.Settings.AllowDemandStart = false;
taskDefinition.Settings.DisallowStartIfOnBatteries = false;
taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
taskDefinition.Settings.StopIfGoingOnBatteries = false;
taskService.RootFolder.RegisterTaskDefinition(@"FOG\" + taskDefinition.RegistrationInfo.Description,
taskDefinition);
taskService.Dispose();
}
示例2: RemoveTask
public void RemoveTask(int min, int hour, bool restart)
{
var taskService = new TaskService();
var task = min + "@" + hour + "@" + ((restart) ? "r" : "s");
taskService.RootFolder.DeleteTask(@"FOG\" + task);
taskService.Dispose();
}
示例3: PersistTask
public static bool PersistTask(bool startAtLogon, bool startForAllUsers)
{
WinTasks.TaskService ts = new WinTasks.TaskService();
try
{
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
bool isElevated = (new WindowsPrincipal(currentIdentity).IsInRole(WindowsBuiltInRole.Administrator));
WinTasks.Task task = ts.FindTask(Application.ProductName, false);
if (startAtLogon)
{
if (startForAllUsers && !isElevated)
{
return PersistTaskElevated(startAtLogon, startForAllUsers);
}
else
{
WinTasks.LogonTrigger trigger = (WinTasks.LogonTrigger)WinTasks.LogonTrigger.CreateTrigger(WinTasks.TaskTriggerType.Logon);
trigger.Enabled = true;
trigger.StartBoundary = DateTime.Today;
if (startForAllUsers)
trigger.UserId = null;
else
trigger.UserId = currentIdentity.Name;
WinTasks.ExecAction action = (WinTasks.ExecAction)WinTasks.ExecAction.CreateAction(WinTasks.TaskActionType.Execute);
action.Path = Application.ExecutablePath;
action.WorkingDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
if (task == null)
{
task = ts.AddTask(Application.ProductName, trigger, action, currentIdentity.Name);
}
else
{
task.Definition.Triggers.Clear();
task.Definition.Triggers.Add(trigger);
task.Definition.Actions.Clear();
task.Definition.Actions.Add(action);
task.RegisterChanges();
}
}
}
else if (task != null)
{
ts.GetFolder("\\").DeleteTask(task.Name);
}
}
catch
{
return false;
}
finally
{
ts.Dispose();
}
return true;
}
示例4: CleanTasks
public static ActionResult CleanTasks(Session session)
{
try
{
var taskService = new TaskService();
var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList();
foreach (var task in existingTasks)
taskService.RootFolder.DeleteTask(@"FOG\" + task.Name);
taskService.RootFolder.DeleteFolder("FOG", false);
taskService.Dispose();
}
catch (Exception)
{
// ignored
}
return ActionResult.Success;
}
示例5: Main
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
string dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
string svc = Path.Combine(dir, "ConnectifyService.exe");
string cli = Path.Combine(dir, "Connectify.exe");
bool isService = false;
if (File.Exists(svc) && File.Exists(cli)) {
RegistryKey runKey = null;
try {
// Remove the client autorun
runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
runKey.DeleteValue("Connectify", false);
} catch (Exception) {
} finally {
if (runKey != null)
runKey.Close();
}
TaskService ts = null;
try {
// Remove service scheduled task if it exists
ts = new TaskService();
foreach (Task t in ts.RootFolder.Tasks) {
if (t.Name.StartsWith("Connectify")) {
ts.RootFolder.DeleteTask(t.Name);
break;
}
}
} catch (Exception) {
} finally {
if (ts != null)
ts.Dispose();
}
ServiceController service = null;
ManagementObject classInstance = null;
ManagementBaseObject inParams = null;
try {
// Set the service to run manually if it exists
service = new ServiceController("Connectify");
if (service != null) {
isService = true;
classInstance = new ManagementObject("root\\CIMV2", "Win32_Service.Name='Connectify'", null);
inParams = classInstance.GetMethodParameters("ChangeStartMode");
inParams["StartMode"] = "Manual";
classInstance.InvokeMethod("ChangeStartMode", inParams, null);
}
} catch (Exception) {
} finally {
if (inParams != null)
inParams.Dispose();
if (classInstance != null)
classInstance.Dispose();
}
try {
if (isService) {
// Stop the service if it happens to be running
service.Stop();
} else {
// Kill services if they happen to be running
foreach (Process p in Process.GetProcessesByName("ConnectifyService")) {
p.Kill();
p.WaitForExit();
}
foreach (Process p in Process.GetProcessesByName("Connectifyd")) {
p.Kill();
p.WaitForExit();
}
}
} catch (Exception) {
}
Process daemon = null;
try {
if (isService) {
// Start the service
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
} else {
// Launch the service & client
daemon = Process.Start(svc);
daemon.WaitForInputIdle();
}
} catch (Exception) {
}
// Launch the client
Process client = Process.Start(cli);
// Wait for client to quit
client.WaitForExit();
try {
if (isService) {
// Stop the service
//.........这里部分代码省略.........
示例6: CloseTaskService
/// <summary>
/// 關閉連線
/// </summary>
private static void CloseTaskService(TaskService ts)
{
ts.Dispose();
}
示例7: ClearAll
public void ClearAll()
{
var taskService = new TaskService();
try
{
taskService.RootFolder.CreateFolder("FOG");
}
catch (Exception)
{
// ignored
}
var existingTasks = taskService.GetFolder("FOG").AllTasks.ToList();
foreach (var task in existingTasks)
{
Log.Debug(LogName, "Delete task " + task.Name);
taskService.RootFolder.DeleteTask(@"FOG\" + task.Name);
}
taskService.Dispose();
}