输出Windows服务已完成的警告消息

时间:2018-10-11 17:43:37

标签: c# windows-services topshelf

我已经用Topshelf和c#开发了Windows服务。它对数据库进行一些查询并写入日志文件。一切正常。但是,我想知道服务何时完成了日志文件的编写,还没有找到一种好的方法。

是否可以通过任何方式显示控制台消息或DOS命令行消息来警告或通知我服务已终止?

服务完成后,我试图向我发送Outlook电子邮件,但是由于公司的限制,我无法从Windows程序或Gmail API发送Outlook电子邮件。

1 个答案:

答案 0 :(得分:0)

建立了答案研究功能: WTSSendMessage

感谢: .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);


        }