subs
subs

Reputation: 2249

windows service to run a method at definite time interval in C++

I want to call a method periodically in a windows service built in C++. I am calling the method in SvcMain().

int main(int argc, char* argv[])
{  
    // The Services to run have to be added in this table.
    // It takes the Service Name and the name of the method to be called by SC Manager.
    // Add any additional services for the process to this table.
    SERVICE_TABLE_ENTRY ServiceTable[]= {{SVCNAME,(LPSERVICE_MAIN_FUNCTION)SvcMain},{NULL,NULL}};

    // This call returns when the service has stopped. 
    // The process should simply terminate when the call returns.
    StartServiceCtrlDispatcher(ServiceTable);  
    return 0;
}

void WINAPI SvcMain(DWORD argc, LPTSTR *argv)
{
    ConnectToServer();
}

Q1. Is this going to fire ConnectToServer() all the time or only once? I just don't know how win service works.
Q2.I want ConnectToServer() to be fired every 15 mins. How can I do that ?

EDIT: How can I create an installer for this service?

Upvotes: 1

Views: 1200

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239636

It's going to call SvcMain once. But you're not doing what you should in SvcMain. There's a good example on MSDN about Writing a ServiceMain function.

If you copy that example, you'd write your code to call ConnectToServer inside the SvcInit function (inside the while(1) loop). You can get the 15 minute delay between calls by specifying 15 minutes as the timeout value in the call to WaitForSingleObject.


If ConnectToServer is a long running process, you should probably find a way of breaking it up and introducing more calls to WaitForSingleObject within it, so that your service responds in a timely fashion to Stop requests.

Upvotes: 1

Related Questions