0

更新:我们仍在工作中使用 XP,我的解决方案正在运行,但现在知道 Vista 及更高版本具有隔离会话,我将实施 WCF IPC...

我有一个 Windows 服务需要通知用户发生某种类型的事件。我认为类似于电子邮件通知消息的东西是有意义的。使用 WPF 做这样一个简单的 UI 也是有意义的。这将使我能够学习一些基础知识。

我运行一个线程:

Thread thread = new Thread(new ThreadStart(RunUserNotificationOnIndependantThread));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

然后我设置对象并调用调用 DoubleAnimation.BeginAnimation 的方法

private void RunUserNotificationOnIndependantThread()
    {
        UserNotificationWithImage test = new UserNotificationWithImage();

        test.Title = _title;
        test.Url = _url;
        test.Message = _message;

        test.LoadUserNotification();
    }

    public void LoadUserNotification()
    {
        Rect workAreaRectangle = System.Windows.SystemParameters.WorkArea;
        Left = workAreaRectangle.Right - Width - BorderThickness.Right;
        Top = workAreaRectangle.Bottom - Height - BorderThickness.Bottom;

        _fadeInAnimation.Completed += new EventHandler(_fadeInAnimation_Completed);

        // Start the fade in animation
        BeginAnimation(UserNotificationBase.OpacityProperty, _fadeInAnimation);
    }

调试器到达 BeginAnimation(...) 并且没有出现任何窗口。这甚至可能还是我在尝试这个时做错了什么???

UserNotification 代码基于 Nicke Andersson 的博客:WPF Desktop Alert blog

谢谢你的帮助!!

4

2 回答 2

4

在 XP 上,与桌面交互的服务有两个严重的问题需要克服 - 没有用户登录时该怎么办和有多个用户登录时该怎么办(快速用户切换和终端服务是最常见的两种登录方式一个以上的用户)。

在 Vista 上,出于安全原因,服务在它们自己的隔离桌面上运行,因此您显示的任何 UI 都将在任何用户无法访问的特殊桌面上运行。

您应该编写一个在用户桌面上运行的小 Gui 程序,并使用某种类型的 IPC(Remoting、Soap、Rest、命名管道、文件,任何您喜欢的)与服务通信。

于 2009-02-11T10:23:52.280 回答
1

一般来说,我根本不推荐 Windows 服务直接与用户的桌面交互。举个简单的例子,出现问题是因为服务可能在任何用户登录之前启动。我的建议是创建一个小型应用程序,它通过用户会话启动并通过 IPC(进程间通信)(如 WCF)与 Windows 服务通信。

但是,如果您确实想尝试让它运行,我的提示是为该服务打开“允许与桌面交互”,我似乎记得这个开关在 Vista 下根本不起作用,但其他人应该确认.

亚历克斯

于 2009-02-11T08:50:56.107 回答