Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
W tych dwóch przykładach języka C++ pokazano, jak wyliczać zadania. W jednym przykładzie pokazano, jak wyświetlać informacje dotyczące zadań w folderze zadań, a inne przykłady pokazują, jak wyświetlić informacje dotyczące wszystkich uruchomionych zadań.
Poniższa procedura opisuje sposób wyświetlania nazw zadań i stanu dla wszystkich zadań w folderze zadań.
Aby wyświetlić nazwy i stan zadań dla wszystkich zadań w folderze zadań
Zainicjuj COM i ustaw ogólne zabezpieczenia COM.
Utwórz obiekt ITaskService.
Ten obiekt umożliwia nawiązywanie połączenia z usługą Harmonogram zadań i uzyskiwanie dostępu do określonego folderu zadań.
Pobierz folder zadań, w którym znajdują się potrzebne informacje.
Aby pobrać folder, użyj metody ITaskService::GetFolder.
Pobierz kolekcję zadań z folderu .
Użyj metody ITaskFolder::GetTasks, aby pobrać kolekcję zadań (IRegisteredTaskCollection).
Pobierz liczbę zadań w kolekcji i wylicz każde zadanie w kolekcji.
Użyj właściwości elementu IRegisteredTaskCollection, aby uzyskać wystąpienie IRegisteredTask. Każde wystąpienie będzie zawierać zadanie w ramach kolekcji. Następnie można wyświetlić informacje (wartości właściwości) z każdego zarejestrowanego zadania.
W poniższym przykładzie języka C++ pokazano, jak wyświetlić nazwę i stan wszystkich zadań w głównym folderze zadań.
/********************************************************************
This sample enumerates through the tasks on the local computer and
displays their name and state.
********************************************************************/
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <comdef.h>
// Include the task header file.
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
using namespace std;
int __cdecl wmain()
{
// ------------------------------------------------------
// Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if( FAILED(hr) )
{
printf("\nCoInitializeEx failed: %x", hr );
return 1;
}
// Set general COM security levels.
hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
0,
NULL);
if( FAILED(hr) )
{
printf("\nCoInitializeSecurity failed: %x", hr );
CoUninitialize();
return 1;
}
// ------------------------------------------------------
// Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService );
if (FAILED(hr))
{
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
CoUninitialize();
return 1;
}
// Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if( FAILED(hr) )
{
printf("ITaskService::Connect failed: %x", hr );
pService->Release();
CoUninitialize();
return 1;
}
// ------------------------------------------------------
// Get the pointer to the root task folder.
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder( _bstr_t( L"\\") , &pRootFolder );
pService->Release();
if( FAILED(hr) )
{
printf("Cannot get Root Folder pointer: %x", hr );
CoUninitialize();
return 1;
}
// -------------------------------------------------------
// Get the registered tasks in the folder.
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks( NULL, &pTaskCollection );
pRootFolder->Release();
if( FAILED(hr) )
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
if( numTasks == 0 )
{
printf("\nNo Tasks are currently running" );
pTaskCollection->Release();
CoUninitialize();
return 1;
}
printf("\nNumber of Tasks : %d", numTasks );
TASK_STATE taskState;
for(LONG i=0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item( _variant_t(i+1), &pRegisteredTask );
if( SUCCEEDED(hr) )
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if( SUCCEEDED(hr) )
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED (hr) )
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
}
}
pTaskCollection->Release();
CoUninitialize();
return 0;
}
Poniższa procedura opisuje sposób wyświetlania nazw zadań i stanu dla wszystkich uruchomionych zadań.
Aby wyświetlić nazwy zadań i stan dla wszystkich uruchomionych zadań
Zainicjować COM i ustawić ogólne zabezpieczenia COM.
Utwórz obiekt ITaskService.
Ten obiekt umożliwia nawiązywanie połączenia z usługą Harmonogram zadań i uzyskiwanie dostępu do określonego folderu zadań.
Użyj metody ITaskService::GetRunningTasks, aby pobrać kolekcję wszystkich uruchomionych zadań (IRunningTaskCollection). Możesz określić, aby uzyskać wystąpienia uruchomionego zadania, włączając lub wykluczając ukryte zadania.
Pobierz liczbę zadań w kolekcji i wylicz każde zadanie w kolekcji.
Użyj właściwości Item IRunningTaskCollection, aby uzyskać wystąpienie IRunningTask . Każda instancja będzie zawierać zadanie w kolekcji. Następnie można wyświetlić informacje (wartości właściwości) z każdego zarejestrowanego zadania.
W poniższym przykładzie języka C++ pokazano, jak wyświetlić nazwę i stan wszystkich uruchomionych zadań, w tym ukrytych zadań.
/********************************************************************
This sample enumerates through all running tasks on the local computer and
displays their name and state.
********************************************************************/
#define _WIN32_DCOM
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <comdef.h>
// Include the task header file.
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
using namespace std;
int __cdecl wmain()
{
// ------------------------------------------------------
// Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if( FAILED(hr) )
{
printf("\nCoInitializeEx failed: %x", hr );
return 1;
}
// Set general COM security levels.
hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
0,
NULL);
if( FAILED(hr) )
{
printf("\nCoInitializeSecurity failed: %x", hr );
CoUninitialize();
return 1;
}
// ------------------------------------------------------
// Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService );
if (FAILED(hr))
{
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
CoUninitialize();
return 1;
}
// Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if( FAILED(hr) )
{
printf("ITaskService::Connect failed: %x", hr );
pService->Release();
CoUninitialize();
return 1;
}
// Get the running tasks.
IRunningTaskCollection* pRunningTasks = NULL;
hr = pService->GetRunningTasks(TASK_ENUM_HIDDEN, &pRunningTasks);
pService->Release();
if( FAILED(hr) )
{
printf("Cannot get Root Folder pointer: %x", hr );
CoUninitialize();
return 1;
}
LONG numTasks = 0;
hr = pRunningTasks->get_Count(&numTasks);
if( numTasks == 0 )
{
printf("\nNo Tasks are currently running" );
pRunningTasks->Release();
CoUninitialize();
return 1;
}
printf("\nNumber of running tasks : %d", numTasks );
TASK_STATE taskState;
for(LONG i=0; i < numTasks; i++)
{
IRunningTask* pRunningTask = NULL;
hr = pRunningTasks->get_Item( _variant_t(i+1), &pRunningTask );
if( SUCCEEDED(hr) )
{
BSTR taskName = NULL;
hr = pRunningTask->get_Name(&taskName);
if( SUCCEEDED(hr) )
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
hr = pRunningTask->get_State(&taskState);
if (SUCCEEDED (hr) )
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRunningTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
}
}
pRunningTasks->Release();
CoUninitialize();
return 0;
}
Tematy pokrewne
-
Korzystanie z harmonogramu zadań