What is the best way to keep a ATL service running

I have created a ATL service and while testing I simply kept it running by leaving a while loop that ran forever (see code below)

HRESULT Run(_In_ int nShowCmd = SW_HIDE)
{
    m_running = true;
    HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, &MyFunctionToRunInTheService, 0, 0, 0);
    while(m_running);
    return CAtlServiceModuleT::Run(nShowCmd);
}

Im now ready to actually run it as a real service but am not sure how I go about doing this? I have looked all over the next and dont seem to be able to find one example of a ATL service.

If I remove the thread code and call the method direct the service is constantly in a start status. If i remove the loop the service simply starts and stops straight away. Any sugestions?

Answers


Ive fixed the problem by changing the code to

HRESULT Run(_In_ int nShowCmd = SW_HIDE)
{
    _beginthreadex(NULL, 0, &CheckForExpiredFiles, 0, 0, 0);
    return CAtlServiceModuleT::Run(nShowCmd);
}

I didnt really need the while loop at all. On further investigation the code to keep the service alive comes as standard. The problem was that the PreMessageLoop method in atlbase.h was returning S_FALSE which casued the service to stop. The reason it returned S_FALSE in my case was becuase I had not added any COM objects. I have therefore overridden the PreMessageLoop method to return S_OK in this case.


Need Your Help

could not access the file imported from iCloud pages

ios8 ios8-extension

I am trying to build an app that imports files from other apps to my app using UIDocumentMenuViewController(in UIDocumentPickerModeImport mode). I could able to do successfully with apps like dropb...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.