I've written an application which writes some data to disk every few seconds to keep the hard drive awake.

Since this application must run all the time I decided to convert it to a Windows Service.

After hours of studying, coding and testing it finally looks as though I've got it right. My service installs, starts, stops etc. My only problem is that the code that writes to disk doesn't work anymore, e.g the file doesn't get written to disk. I really have no idea what the problem could be.

The code is contained in a thread:

PHP Code:
unsigned __stdcall ServiceWorkerThread(void pVoid)
{
    
endWorkerThread = ::CreateEvent(0TRUEFALSE0);
    
    
long data 1000;
    
    
serviceRunning true;

    while (
serviceRunning == true)
    {
        
std::wofstream dfile;
        
dfile.open(L"data.txt"std::ios::out);
        
data data 1;
        
dfile << data;
        
dfile.close();
        
Sleep(25000);
    }
    ::
SetEvent(endWorkerThread);
    return 
0;

The thread is started as follows:

PHP Code:
void StartWorkerThread()
{
    
hThread reinterpret_cast<HANDLE>(::_beginthreadex(NULL,
                 
NULL,
                 
ServiceWorkerThread,
                 
NULL,
                 
NULL,
                 
NULL));
    return;

I've tried debugging the service and I can see that the thread gets called and I can step through it, but wofstream looks extremely complex and I can't make out whats going on inside it.

So what's causing the problem? Is it the thread or the service part, or both?

If anyone can shed some light on this it will be greatly appreciated.