About building a Windows service
-
I am about to implement a windows background service. I have succeeded to build the basic functions such as creating, stopping and deleting the service. The service is actually built basing on an infinite while loop (while(1)), if i put some simply operation like file processing in the while loop, it works fine~ when i put this line "doc. getdocumentlist(doclist, 9);" in, the program just stops there and cannot proceed to the codes below( this can be seen from the log file content). Can anybody explain this to me...
void cService::EndlessLoop() { Output("EndlessLoop Start\n"); SYSTEMTIME Clock; CString Out; Cdocumentdb doc; // This endlessloop is killed by Windows when the service is stopped or shutdown while (1) { if (hTimer != 0) { LARGE_INTEGER Elapse; Elapse.QuadPart = -10000000; // 5 Seconds in 100 Nanoseconds resolution (negative=relative) BOOL Res = ::SetWaitableTimer(hTimer, &Elapse, 0, 0, 0, TRUE); if (Res) { DWORD Event = WaitForSingleObject(hTimer, 30000); // 30 seconds timeout // Event = (258=WAIT_TIMEOUT) (128=WAIT_ABANDONED) (0=WAIT_OBJECT_0) (0xFFFFFFFF=WAIT_FAILED) } } CArray doclist; **doc. getdocumentlist(doclist, 9);** ::GetLocalTime(&Clock); Out.Format("Timer %02d:%02d:%02d:%03d", Clock.wHour, Clock.wMinute, Clock.wSecond, Clock.wMilliseconds); Output(Out); }; }
-
I am about to implement a windows background service. I have succeeded to build the basic functions such as creating, stopping and deleting the service. The service is actually built basing on an infinite while loop (while(1)), if i put some simply operation like file processing in the while loop, it works fine~ when i put this line "doc. getdocumentlist(doclist, 9);" in, the program just stops there and cannot proceed to the codes below( this can be seen from the log file content). Can anybody explain this to me...
void cService::EndlessLoop() { Output("EndlessLoop Start\n"); SYSTEMTIME Clock; CString Out; Cdocumentdb doc; // This endlessloop is killed by Windows when the service is stopped or shutdown while (1) { if (hTimer != 0) { LARGE_INTEGER Elapse; Elapse.QuadPart = -10000000; // 5 Seconds in 100 Nanoseconds resolution (negative=relative) BOOL Res = ::SetWaitableTimer(hTimer, &Elapse, 0, 0, 0, TRUE); if (Res) { DWORD Event = WaitForSingleObject(hTimer, 30000); // 30 seconds timeout // Event = (258=WAIT_TIMEOUT) (128=WAIT_ABANDONED) (0=WAIT_OBJECT_0) (0xFFFFFFFF=WAIT_FAILED) } } CArray doclist; **doc. getdocumentlist(doclist, 9);** ::GetLocalTime(&Clock); Out.Format("Timer %02d:%02d:%02d:%03d", Clock.wHour, Clock.wMinute, Clock.wSecond, Clock.wMilliseconds); Output(Out); }; }
-
sorry, forgot to say, what the "doc. getdocumentlist(doclist, 9);" is to retrieve some information from a database table...
Does your code work when it is NOT placed into a service? If that is true, then it could be a permissions issue. You might need to make sure the service runs under an account that has the proper privileges and security to access the database.