views:

608

answers:

2

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...

I have a windows service that needs to notify the user of an event of some type occurring. I decided that something similar to email notification messages would make sense. It also makes sense to do such a simple UI using WPF. 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

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. 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

Thanks for any help!!

+1  A: 

Generally speaking I would not recommend a Windows Service to interact with the user's desktop directly at all. 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.

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.

HTH alex

AlexDuggleby
+3  A: 

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).

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.

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).

Nir