Delmonte
Delmonte

Reputation: 411

Output a warning message that windows service has finished

I have develop a windows service with Topshelf and c#. It makes some queries to the database and writes a log file. It's working fine. However, I would like to know when the service has finished to write the log file and haven't find yet a good way to do it.

Is it any way to show a console message or DOS command line message to warn or inform me that the service has ended?

I tried to send me an Outlook email when the service has finished, but because of my company's restrictions, I can't send Outlook emails from Windows programs or use Gmail API.

Upvotes: 0

Views: 368

Answers (1)

Delmonte
Delmonte

Reputation: 411

Found the answer researching function: WTSSendMessage

Thanks to: .NET Windows Services (WTSSendMessage) : Displays message on XP but not Windows 7

       static extern bool WTSSendMessage(
              IntPtr hServer,
              [MarshalAs(UnmanagedType.I4)] int SessionId,
              String pTitle,
              [MarshalAs(UnmanagedType.U4)] int TitleLength,
              String pMessage,
              [MarshalAs(UnmanagedType.U4)] int MessageLength,
              [MarshalAs(UnmanagedType.U4)] int Style,
              [MarshalAs(UnmanagedType.U4)] int Timeout,
              [MarshalAs(UnmanagedType.U4)] out int pResponse,
              bool bWait);

    [DllImport("Kernel32.dll", SetLastError = true)]
    static extern int WTSGetActiveConsoleSessionID();

    public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
    public static int WTS_CURRENT_SESSION = 1;

        public static void Show_Message()
        {
            bool result = false;
            string title = "Aviso";
            int tlen = title.Length;
            string msg = "Service ended !";
            int mlen = msg.Length;
            int resp = 0;

            result = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, title, tlen, msg, mlen, 0, 0, out resp, true);
            //int err = Marshal.GetLastWin32Error();
            //System.Console.WriteLine("result:{0}, errorCode:{1}, response:{2}", result, err, resp);


        }

Upvotes: 1

Related Questions