简体   繁体   English

Windows Service通过WPF Outlook样式控件通知用户不可见

[英]Windows Service notifying user with WPF outlook style control not becoming visible

Update: We are still using XP at work and I got my solution working, but now knowing that Vista and beyond have the isolated session I am going to implement a WCF IPC... 更新:我们仍在使用XP,而且我的解决方案也可以使用,但是现在知道Vista和以后的会议都是孤立的,因此我将实现WCF IPC。

I have a windows service that needs to notify the user of an event of some type occurring. 我有一个Windows服务,需要通知用户某种类型的事件正在发生。 I decided that something similar to email notification messages would make sense. 我认为类似电子邮件通知消息的内容很有意义。 It also makes sense to do such a simple UI using WPF. 使用WPF做这样一个简单的UI也很有意义。 This would allow me to learn some basics. 这将使我学习一些基础知识。

I run a thread: 我运行一个线程:

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

Then I set up the object and call the method that calls DoubleAnimation.BeginAnimation 然后设置对象并调用调用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);
    }

The debugger reaches BeginAnimation(...) and no window appears. 调试器到达BeginAnimation(...),并且没有窗口出现。 Is this even possible or what am I doing wrong in attempting this??? 这甚至有可能还是我在尝试此操作时做错了什么???

The UserNotification code is based on a blog by Nicke Andersson: WPF Desktop Alert blog UserNotification代码基于Nicke Andersson的博客: WPF Desktop Alert博客

Thanks for any help!! 谢谢你的帮助!!

On XP a service that interact with the desktop has two serious problems to overcome - what to do when no users are logged in and what to do when several user are logged in (fast user switching and terminal services are the two most common ways to log in more then one user). 在XP上,与桌面交互的服务有两个要解决的严重问题-没有用户登录时该怎么办以及几个用户登录时该怎么办(快速用户切换和终端服务是两种最常见的登录方式多于一个用户)。

On Vista, for security reasons, services run on their own isolated desktop so any UI you show will go on that special desktop that no user can ever access. 在Vista上,出于安全原因,服务在它们自己的隔离桌面上运行,因此您显示的任何UI都将在该特殊桌面上运行,任何用户都无法访问。

You should write a small Gui program that runs on the user's desktop and communicate with the service using some type of IPC (Remoting, Soap, Rest, named pipes, files, whatever you like). 您应该编写一个小型的Gui程序,该程序在用户的桌面上运行,并使用某种IPC(远程处理,Soap,Rest,命名管道,文件,随便什么)与服务进行通信。

Generally speaking I would not recommend a Windows Service to interact with the user's desktop directly at all. 一般来说,我根本不建议Windows服务直接与用户的桌面进行交互。 As a simple example, problems arise because the service may start before any user is logged on. 举一个简单的例子,出现问题是因为该服务可能在任何用户登录之前启动。 My suggestion would be to create a small app that start-up with the user session and communicated to the Windows Service via IPC (Interprocess Communication) such as WCF. 我的建议是创建一个小型应用程序,该应用程序以用户会话启动,并通过IPC(进程间通信)(例如WCF)与Windows Service通信。

But if you do want to try to get it running, my hint would be switch on "Allow interaction with desktop" for the service and I seem to remember that this switch doesn't work at all under Vista, but someone else should confirm that. 但是,如果您想尝试使其运行,我的提示将是打开该服务的“允许与桌面交互”,我似乎还记得该开关在Vista中根本不起作用,但其他人应该确认。

HTH alex HTH Alex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM