Windows服务未显示弹出窗口

时间:2013-12-27 04:54:24

标签: c# windows-services popupwindow

我创建了一个Windows服务,可以在特定时间启动WPF弹出窗口。该服务在调试模式下工作正常,但是当我安装该服务时,它不显示弹出窗口,但它会在弹出命令后执行命令。我尝试允许该服务与桌面交互,但这也无法正常工作。在此代码中,关闭机器的行在安装服务后执行,但它没有启动可执行文件(这是WPF弹出窗口)。

    Process p = new Process();
    System.Timers.Timer _timer;
    int intervalMins = 10;
    bool IsInvokedToday = false;
    DateTime startAt = DateTime.Now.AddHours(16);

    public Shutdownservice()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        _timer = new Timer(intervalMins * 1000);
        _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        _timer.Start();
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (DateTime.Now > DateTime.Today.Add(new TimeSpan(16, 0, 0)))
        {
            countForNoResponse();
        }
    }

    protected override void OnStop()
    {
    }

    public void countForNoResponse()
    {
        Process p = new Process();

        int count = 0;
        while (count < 4)
        {
            p.StartInfo = new ProcessStartInfo(@"C:\Windows\Temp\ShutdownSystem.exe");
            p.Start();
            bool wait = p.WaitForExit(1000 * 5);
            if (!wait)
            {
                p.Kill();
                System.Threading.Thread.Sleep(1000 * 5);
                count++;
                if (count == 4)
                {
                    Process.Start("shutdown", "/s /t 900");
                }
            }
            else
            {
                count = 6;
            } 
        }
    }

1 个答案:

答案 0 :(得分:0)

不要尝试从Windows服务显示UI(即使通过启动单独的进程) 这从来都不容易,因为默认情况下禁用Windows 8交互式服务(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\NoInteractiveServices == 1)。

使用单独的GUI应用程序,该应用程序在用户登录时运行并侦听服务消息。