Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have this window service created in c#4.0 .net and using the api of twilio.
this will send a text message to my phone if a server was shutdown.
after i installed the service and tried to shutdown my pc but nothing happens.
I am not receiving any text at all.

protected override void OnStart(string[] args)
{
    SystemEvents.SessionEnding += OnSessionEnding;
    SystemEvents.SessionEnded += OnSessionEnded;
    eventLog1.WriteEntry("starting");
}

protected override void OnStop()
{
    SystemEvents.SessionEnding -= OnSessionEnding;
    SystemEvents.SessionEnded -= OnSessionEnded;

    eventLog1.WriteEntry("stopping");
}


protected static void OnSessionEnding(Object sender, SessionEndingEventArgs e)
{
    if (e.Reason == SessionEndReasons.SystemShutdown)
    {

        string AccountSid = "[myaccount sid]";
        string AuthToken = "[auth_token]";

        var twil = new TwilioRestClient(AccountSid, AuthToken);

        var message = twil.SendMessage("[fromnumber]", "[tonumber]",         "Server shutting down'" + Environment.MachineName + "'");

        Console.WriteLine(message.Sid);

    }
}

protected static void OnSessionEnded(Object sender, SessionEndedEventArgs e)
{
    if (e.Reason == SessionEndReasons.SystemShutdown)
    {
        string AccountSid = "[myaccount sid]";
        string AuthToken = "[auth_token]";

        var twil = new TwilioRestClient(AccountSid, AuthToken);

        var message = twil.SendMessage("[fromnumber]", "[tonumber]", "Server has been shutdown '" + Environment.MachineName + "'");
        Console.WriteLine(message.Sid);

    }
}


please advise.. Thank you.
Posted
Comments
[no name] 13-Mar-15 11:42am    
I assume OnStop() is called before the event SessionEnding occurs. So your OnSessionEnding-Eventhandler-Method never gets called. You could verify that by inserting a call to eventLog1.WriteEntry into OnSessionEnding.
etolf 13-Mar-15 13:22pm    
which one should be use ended or ending?
[no name] 13-Mar-15 13:26pm    
I would suggest to try first commenting out the event-unsubscribing in OnStop() and see what happens.
etolf 13-Mar-15 13:57pm    
ok will try that one thanks
etolf 19-Mar-15 5:12am    
done unsubscribing in on stop() still nothing happens

1 solution

 
Share this answer
 
Comments
Joan Magnet 13-Mar-15 12:36pm    
It isn't a good idea to let a server control himself.
Assign this task to another server.
One ring to rule them all.
etolf 13-Mar-15 13:19pm    
what do you mean?I just want to put a window service that if a pc shutsdown it will send me a message.
Joan Magnet 13-Mar-15 16:25pm    
Another server can detect that your server no longer responds and then, send you a message
Andy Lanng 16-Mar-15 6:45am    
Agreed - and in practice this is what I do.
etolf 19-Mar-15 4:52am    
if this possible when the pc is on from the state from being shutdown and the service will start then i will send a message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900