StartScheduler

This forum is designated to discuss SiComponents Scheduling Agent.
Post Reply
Ted Tripp
Posts: 5
Joined: Fri Aug 18, 2006 2:04 am

StartScheduler

Post by Ted Tripp »

I would like to give the user the option to start the scheduling service if it is not already started. StartScheduler, Open and Active all seem to start the scheduler (if it can be started), but none seem to simply check the status.
isiticov
Site Admin
Posts: 2383
Joined: Thu Nov 21, 2002 3:17 pm

Post by isiticov »

Add WinSvc in uses and use the following:

Code: Select all

function SchedulerServiceRunning: Boolean;
const
  SCHED_CLASS = 'SAGEWINDOWCLASS';
  SCHED_TITLE = 'SYSTEM AGENT COM WINDOW';
  SCHED_SERVICE_APP_NAME = 'mstask.exe';
  SCHED_SERVICE_NAME = 'Schedule';
var
  hSC: SC_HANDLE;
  hSchSvc: SC_HANDLE;
  SvcStatus: TServiceStatus;
begin
  Result := False;
  if SysUtils.Win32Platform = VER_PLATFORM_WIN32_WINDOWS then
  begin
    Result := FindWindow(SCHED_CLASS, SCHED_TITLE) <> 0;
  end
  else
  begin
    hSC := OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
    if hSC = 0 then
      Result := GetLastError = 0
    else
    begin
      hSchSvc := OpenService(hSC, SCHED_SERVICE_NAME, SERVICE_QUERY_STATUS);
      if hSchSvc = 0 then
        Exit;
      CloseServiceHandle(hSC);
      if not QueryServiceStatus(hSchSvc, SvcStatus) then
      begin
        CloseServiceHandle(hSchSvc);
        Exit;
      end
      else
      begin
        if SvcStatus.dwCurrentState = SERVICE_RUNNING then
        begin
          CloseServiceHandle(hSchSvc);
          Result := True;
        end
        else
          CloseServiceHandle(hSchSvc);
      end;
    end;
  end;
end;
Best regards,
Igor Siticov.
Ted Tripp
Posts: 5
Joined: Fri Aug 18, 2006 2:04 am

Post by Ted Tripp »

A belated thanks
Post Reply