简体   繁体   中英

Window is not visible when started from a Windows Service

There is a windows service that monitors a given infrastructure. Under some conditions there is a need to pop up a form where the user see some indicators and able to do some choice.

For this purpose I am staring a WPF Window

var thread = new Thread(() =>
{

    var w = new MyWindow();
    _uis.Add(w);
    w.Info = sb.ToString();
    w.Show();

            System.Windows.Threading.Dispatcher.Run();
});

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

and unfortunatelly cannot see the window. When I am hosting my service busines logic in a console application I can see the window.

Where is the trick?

Services do not have access to the same privileges (Network Service/Local Service) and do not have access to your desktop.

Services are meant to be silent in nature by design and simply do not have the ability to launch windows.

A console application does have this ability, console applications are not windows services and of course do have the ability to interact with a user.

You could split the functionality:

  • service to do the monitoring
  • client application to display signals from the service

The client app could simply listen to a socket for messages from the service that should be displayed to the user.

The service will start up when the machines starts. The app will start with every user who logs in.

There is no trick. Don't do it

This approach is against well established practices and patterns regarding services and UI. You should not attempt to show a UI (or an interactive UI) directly from a windows service.

The standard practice is to write a separate UI application that communicates with your windows service. You can consider periodically updating the UI Webservices can be used to communicate between the service and the UI application

The hard way

However if you really want to do what you originally asked, here you go

http://www.codeproject.com/Articles/35773/Subverting-Vista-UAC-in-Both-32-and-64-bit-Archite

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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