This project has moved and is read-only. For the latest updates, please go here.

How to disable task scheduler in c#

Topics: Errors
Aug 26, 2013 at 3:19 PM
Hi all,

Hope doing well,
sir i am writing a code where i need to disable the particular task in task scheduler.
Here is my code:

string taskName = "Device";
        TaskService ts = new TaskService(@"\\VIKASH", "VICKY", "VIKASH", "securax", true);
        using ( ts = new TaskService())
        {

            Task t = ts.GetTask(taskName);
            if (t != null)
            {
                if (t.Enabled)
                {
                    t.Enabled = false;
                    t.RegisterChanges();
                }
            }
        }
but getting error in this line " t.Enabled = false;"
Error is Access is denied Exception from HResult: 0x80070005 (E_AccessDenied)

how to disable it?
Thanks
Aug 28, 2013 at 12:35 AM
This exact question has been asked and answered: https://taskscheduler.codeplex.com/discussions/442540.
Aug 29, 2013 at 12:23 PM
Hi all,

the issue is fixed but one other error is coming after using this function in other function of application getting error "safe handle has been closed".

how to resolve this issue.

Thanks
Aug 29, 2013 at 4:42 PM
Edited Aug 29, 2013 at 4:43 PM
Typically this means that you have disposed of the object and then are trying to use it again. Look out for the use of "using" statements. Once a using block is complete, the underlying COM objects are released and you should not use references to the library object any longer.

For example, the code you posted above uses a using statement incorrectly and will result in errors when using the ts variable after the using block.
Aug 30, 2013 at 5:15 AM
Hi sir,

Instead of using keyword what code we can use?

can u share some code here?

Thanks
Aug 30, 2013 at 7:58 AM
Hi sir,

Discribing some more about this issue.

i copied the stack trace here which is below:

at System.Security.Principal.Win32.ImpersonateLoggedOnUser(SafeTokenHandle hToken)\r\n at System.Security.Principal.WindowsIdentity.SafeImpersonate(SafeTokenHandle userToken, WindowsIdentity wi, StackCrawlMark& stackMark)\r\n at System.Security.Principal.WindowsIdentity.Impersonate(StackCrawlMark& stackMark)\r\n at System.Security.SecurityContext.SetSecurityContext(SecurityContext sc, SecurityContext prevSecurityContext, StackCrawlMark& stackMark)\r\n at System.Threading.ExecutionContext.SetExecutionContext(ExecutionContext executionContext)\r\n at System.Threading.ExecutionContext.runTryCode(Object userData)\r\n at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading._TimerCallback.PerformTimerCallback(Object state)"
Sep 3, 2013 at 9:44 PM
If you are putting TaskService inside a using clause like the following, you cannot use the variable outside the brackets.
using (TaskService ts = new TaskService())
{
   // All references to "ts" must be here. Outside of the brackets for the using clause, any reference to "ts" will cause errors.
}
If you hold onto an instance of TaskService, then you must instantiate it and dispose of it manually.
class MyClass : IDisposable
{
   private TaskService ts;

   public MyClass()
   {
      ts = new TaskService();
   }

   void Dispose()
   {
      ts.Dispose();
      ts = null;
   }

   public void DoSomething()
   {
      ts.RootFolder.CreateFolder("NewFolder");
   }
}