develooper Front page | perl.libwin32 | Postings from June 2011

Suggested correction re Win32::Daemon detail

From:
Peter Stevens
Date:
June 29, 2011 00:51
Subject:
Suggested correction re Win32::Daemon detail
Message ID:
5BC8191CB8F76F46A0ECA6B19F16347501534A790D@MELBEXCH1.aemo.local
    use Win32::Daemon;
    Win32::Daemon::RegisterCallbacks( {
        start       =>  \&Callback_Start,
        running     =>  \&Callback_Running,
        stop        =>  \&Callback_Stop,
        pause       =>  \&Callback_Pause,
        continue    =>  \&Callback_Continue,
    } );

    %Context = (
        last_state => SERVICE_STOPPED,
        start_time => time(),
    );
    
    # Start the service passing in a context and
    # indicating to callback using the "Running" event
    # every 2000 milliseconds (2 seconds).
    Win32::Daemon::StartService( \%Context, 2000 );
    
    sub Callback_Running
    {
        my( $Event, $Context ) = @_;
        
        # Note that here you want to check that the state
        # is indeed SERVICE_RUNNING. Even though the Running
        # callback is called it could have done so before 
        # calling the "Start" callback.
        if( SERVICE_RUNNING == Win32::Daemon::State() )
        {
            # ... process your main stuff here...
            # ... note that here there is no need to
            #     change the state
        }
		elsif (SERVICE_PENDING_CONTINUE == Win32::Daemon::State() )
		{
			Win32::Daemon::State( SERVICE_RUNNING );
		}
		else
		{
		}

    sub Callback_Start
    {
        my( $Event, $Context ) = @_;
        # Initialization code
        # ...do whatever you need to do to start...

        $Context->{last_state} = SERVICE_RUNNING;
        Win32::Daemon::State( SERVICE_RUNNING );
    }

    sub Callback_Pause
    {
        my( $Event, $Context ) = @_;
        $Context->{last_state} = SERVICE_PAUSED;
        Win32::Daemon::State( SERVICE_PAUSED );
    }

    sub Callback_Continue
    {
		if( SERVICE_CONTINUE_PENDING == Win32::Daemon::State() )
		{
			my( $Event, $Context ) = @_;
			$Context->{last_state} = SERVICE_RUNNING;
			Win32::Daemon::State( SERVICE_RUNNING );
		}
		else
		{
			Win32::Daemon::State(SERVICE_CONTINUE_PENDING);
		}
	}

    sub Callback_Stop
    {
        my( $Event, $Context ) = @_;
        $Context->{last_state} = SERVICE_STOPPED;
        Win32::Daemon::State( SERVICE_STOPPED );
        
        # We need to notify the Daemon that we want to stop callbacks and the service.
        Win32::Daemon::StopService();
    }



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About