none
请教一个taskschedule的问题 RRS feed

  • 问题

  • 我在管理员用户下创建了一个taskschedule任务,每个用户登录后都会执行。现在的情况是当登录guest用户后不log off,切换到其他用户后,在后切换的用户下这个任务就会大概10分钟以后才启动。而guest没有登录的情况下就不会出现这种问题,请哪位指教一下是什么原因?

    2008年8月12日 3:11

答案

全部回复

  •  

    如何创建的这个 task schedule ?  是以什么身份执行的?

     

    schedule的设置呢?

    2008年8月12日 9:06
  •  

    如果以administrators组用户,运行了?是什么情况。guest帐户未开启,一切正常
    2008年8月12日 11:57
  • 这部分就是创建任务的代码,我手工创建的task schedule 的任务设置和这个一样。

    BOOL CreateTask()
    {
     BOOL bRet = FALSE;

     TCHAR AppPath[MAX_PATH] = {0};

     // get current app path
     ::GetCurrentDirectory(MAX_PATH, AppPath);
     CString strAppPath(AppPath);
     strAppPath += APP_NAME;

        //for windows Vista or later OS and admin
     if ( IsVistaorLater() && IsAdmin() )
     {
      //  Initialize COM.
      ::CoInitialize(NULL);

      // Provides access to the Task Scheduler service for managing registered tasks.
      CComPtr<ITaskService> pService;
      HRESULT hr = CoCreateInstance(CLSID_TaskScheduler,
               NULL,
               CLSCTX_INPROC_SERVER,
               IID_ITaskService,
               (void**)&pService);
      if ( SUCCEEDED(hr) )
      {
       //  The Connect method should be called before calling any of the other ITaskService methods.
       // Connects to a remote computer and associates all subsequent calls on this interface
       // with a remote session. If the serverName parameter is empty, then this method will
       // execute on the local computer. If the userId is not specified, then the current token is used.
       hr = pService->Connect(CComVariant(), CComVariant(), CComVariant(), CComVariant());
       if ( SUCCEEDED(hr) )
       {
        // Provides the methods that are used to register (create) tasks in the folder,
        // remove tasks from the folder, and create or remove subfolders from the folder.
        CComPtr<ITaskFolder> pRootFolder;

        //Gets a folder that contains tasks at a specified location
        hr = pService->GetFolder(CComBSTR(_T("\\")), &pRootFolder);
        if ( SUCCEEDED(hr) )
        {
         //Get SONY folder
         CComPtr<ITaskFolder> pSonyFolder;
         hr = pRootFolder->GetFolder(CComBSTR(SONY), &pSonyFolder);
         if ( FAILED(hr) )
         {
          //Create SONY folder
          pRootFolder->CreateFolder(CComBSTR(SONY), CComVariant(), &pSonyFolder);
         }

         CComPtr<ITaskFolder> pPYVFolder;
         hr = pSonyFolder->GetFolder(CComBSTR(TASK_NAME), &pPYVFolder);
         if ( FAILED(hr) )
         {
          // Creates a folder for related tasks
          pSonyFolder->CreateFolder(CComBSTR(TASK_NAME), CComVariant(), &pPYVFolder);
         }

         //  If the same task exists, remove it.
         pPYVFolder->DeleteTask(CComBSTR(TASK_NAME), 0);

         // Defines all the components of a task, such as the task settings,
         // triggers, actions, and registration information.
         CComPtr<ITaskDefinition> pTask;

         // Returns an empty task definition object to be filled in with settings and properties
         // and then registered using the ITaskFolder::RegisterTaskDefinition method.
         hr = pService->NewTask(0, &pTask);
         if ( SUCCEEDED(hr) )
         {
          // Provides the security credentials for a principal. These security credentials
          // define the security context for the tasks that are associated with the principal.
          CComPtr<IPrincipal> pPrincipal;
          hr = pTask->get_Principal(&pPrincipal);
          if ( SUCCEEDED(hr) )
          {
           // Defines LUA elevation flags that specify with what privilege level the task will be run.
           // TASK_RUNLEVEL_HIGHEST:Tasks will be run with the highest privileges.
           hr = pPrincipal->put_RunLevel(TASK_RUNLEVEL_HIGHEST);
           if ( SUCCEEDED(hr) )
           {
            hr = pTask->put_Principal(pPrincipal);
            if ( SUCCEEDED(hr) )
            {
             //  Get the registration info for setting the identification.
             CComPtr<IRegistrationInfo> pRegInfo;
             pTask->get_RegistrationInfo(&pRegInfo);
             pRegInfo->put_Author((CComBSTR)SONY);

             //  Create the settings for the task
             CComPtr<ITaskSettings> pSettings;
             pTask->get_Settings(&pSettings);
             pSettings->put_DisallowStartIfOnBatteries(VARIANT_BOOL(FALSE));//auto run even OnBatteries
             pSettings->put_StopIfGoingOnBatteries(VARIANT_BOOL(FALSE));
             pSettings->put_ExecutionTimeLimit( CComBSTR(_T("PT0S")));//never auto end
             pSettings->put_MultipleInstances(TASK_INSTANCES_PARALLEL);
             pSettings->put_AllowDemandStart(TRUE);
             pSettings->put_AllowHardTerminate(TRUE);

             //  Set setting values for the task.
             hr = pSettings->put_StartWhenAvailable(VARIANT_BOOL(TRUE));
             if ( SUCCEEDED(hr) )
             {
              //  Get the trigger collection to insert the logon trigger.
              CComPtr<ITriggerCollection> pTriggerCollection;
              hr = pTask->get_Triggers(&pTriggerCollection);
              if ( SUCCEEDED(hr) )
              {
               //  Add the logon trigger to the task.
               CComPtr<ITrigger> pTrigger;
               hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger);
               if ( SUCCEEDED(hr) )
               {
                CComPtr<ILogonTrigger> pLogonTrigger;  
                pTrigger->QueryInterface(IID_ILogonTrigger, (void**)&pLogonTrigger);
                pLogonTrigger->put_Delay(CComBSTR(_T("PT5S")));
                pLogonTrigger->put_Id( CComBSTR( TASK_NAME ) );

                //  Add an Action to the task. This task will execute 
                CComPtr<IActionCollection> pActionCollection;
                hr = pTask->get_Actions(&pActionCollection);
                if ( SUCCEEDED(hr) )
                {
                 //  Create the action, specifying that it is an executable action.
                 CComPtr<IAction> pAction;
                 hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction);
                 if ( SUCCEEDED(hr) )
                 {
                  // Represents an action that executes a command-line operation.
                  CComPtr<IExecAction> pExecAction;
                  //  QI for the executable task pointer.
                  hr = pAction->QueryInterface(IID_IExecAction,
                   (void**)&pExecAction);
                  if ( SUCCEEDED(hr) )
                  {
                   // set app path and arguments
                   pExecAction->put_Path(CComBSTR(strAppPath.AllocSysString()));
                   pExecAction->put_Arguments(CComBSTR(_T("")));
                   // Provides the methods that are used to run the task immediately,
                   // get any running instances of the task, get or set the credentials
                   // that are used to register the task, and the properties that describe the task.
                   CComPtr<IRegisteredTask> pRegisteredTask;
                   hr = pPYVFolder->RegisterTaskDefinition(
                    CComBSTR(TASK_NAME),
                    pTask,
                    TASK_CREATE_OR_UPDATE,
                    CComVariant(_T("S-1-5-32-545")),
                    CComVariant(),
                    TASK_LOGON_GROUP,
                    CComVariant(_T("")),
                    &pRegisteredTask);
                   if ( SUCCEEDED(hr) )
                   {
                    bRet = TRUE;
                   }
                  }               
                 }              
                }             
               }            
              }           
             }
            }        
           }        
          }       
         }     
        }     
       }
      }
     }
     ::CoUninitialize();

     return FALSE;
    }

    2008年8月13日 8:19
  • ебануться....фанатам ссылок-пасымок microsoft посвящяется :(

    2014年3月29日 0:20