Hello all,

I am working on an application which I originally wrote as a console app and am now transforming into a Windows service. Basically, looking for a little advice. 

My goal is to have an independent service which starts at a specific time and only runs for a set amount of hours. The code I am currently contemplating looks like this:

----------------------------------------------------------------
   int start_hour = 18; //6pm

    time_t t = time(0); //current time
    struct tm* now = localtime ( &t );

    //total time the app should run for
    boost::posix_time::time_duration run_duration ( 10, 0, 0, 0 );

    //current time relative duration
    boost::posix_time::time_duration start_duration ( now->tm_hour, now->tm_min,  now->tm_sec );

    //get duration until service start
    boost::posix_time::time_duration diff = boost::posix_time::hours(start_hour) - boost::posix_time::milliseconds (start_duration.total_milliseconds() );

    //dont start thread until designated start time
    boost::thread thr ( &ServiceWorkerThread );

    //sleep this thread until ready to start
    boost::this_thread::sleep( boost::posix_time::milliseconds( diff.total_milliseconds() ) );

    if ( thr.timed_join( run_duration ) )
    {
        //do stuff
    }

-------------------------------------------------------------

Basically, I am trying to make use of the time_duration class to achieve this. Based on the code above, I want to run the service for 10 hours, but not start it until the correct hour.

Somewhere near the end of the service entry I will restart the service and the service *should* until the correct time and only execute for X hours.

I am looking for further suggestions/improvements based on the above. 

Thanks!