i thought about starting a worker thread in Run() then making that a while(SERVICE_RUNNING) and then at the top of the while do a Sleep(300000) so each time round it waits 300 seconds before going round again.
Main Topics
Browse All TopicsUsing the ATL Wizard in VC++6 I've created a service. I'm guessing that my code has to go in the run() method.
My first question is:
If all I want my service to do is after a set time interval get some data from the database. How do I loop?
If I put a while loop in the run method then i will effectively lock the service from communicating with the SCM.
Any help or small examples would be great, I do have a book on it but as of yet I've not found anything regarding this, and I am pushed for time.
Second question:
Can ATL system services support MFC class usage?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
oops, I see that the Support MFC checkbox gets dimmed when you select the Service radio button. Looking at my code, for a service that uses MFC, I see that I needed to add some headers to StdAfx.h:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
I also see that for some reason I have this in the InitInstance:
afxContextIsDLL= FALSE;
Alas, I did not comment the reason for that code!
-- Dan
BTW, if you'd like something more sophisticated than the above, check out http://support.microsoft.c
one last thing :-)
below is the Run() method.
I have put '*****************' where i think my code should go, is this right???
void CServiceModule::Run()
{
_Module.dwThreadID = GetCurrentThreadId();
HRESULT hr = CoInitialize(NULL);
// If you are running on NT 4.0 or higher you can use the following call
// instead to make the EXE free threaded.
// This means that calls come in on a random RPC thread
// HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
_ASSERTE(SUCCEEDED(hr));
// This provides a NULL DACL which will allow access to everyone.
CSecurityDescriptor sd;
sd.InitializeFromThreadTok
hr = CoInitializeSecurity(sd, -1, NULL, NULL,
RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONAT
_ASSERTE(SUCCEEDED(hr));
hr = _Module.RegisterClassObjec
_ASSERTE(SUCCEEDED(hr));
LogEvent(_T("Service started"));
if (m_bService)
SetServiceStatus(SERVICE_R
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
DispatchMessage(&msg);
**************************
_Module.RevokeClassObjects
CoUninitialize();
}
Yes, but the docs to 'SetTimer()' state:
"Remarks
An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER."
If you don't have any UI, there will be no msg processing/dispatching...
Dear MattC
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity, I will suggest to split between
"jkr, DanRollins, cookre"
comment(s) as an answer.
If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchang
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Business Accounts
Answer for Membership
by: cookrePosted on 2002-03-31 at 16:06:30ID: 6909448
There are several ways:
Use a timer to activate a function at a specified interval:
#define MonitorID 1
UINT MonitorTimer;
MonitorTimer=SetTimer(hWnd
,MonitorID
,10000 // (10 second interval)
,(TIMERPROC)NSEMonitor);
void CALLBACK NSEMonitor(HWND hwnd
,UINT uMsg
,UINT_PTR idEvent
,DWORD dwTime)
With this method, your program can go about others chores and the call back proc will be triggered at the specified intervals.
The other way is with a simple Sleep():
Sleep(2000);
This will pause the entire process, but won't effect the performance of the OS.