Yaugen Vlasau
Yaugen Vlasau

Reputation: 2218

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?

Upvotes: 0

Views: 1182

Answers (3)

Sunil Purushothaman
Sunil Purushothaman

Reputation: 9521

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

Upvotes: 1

Emond
Emond

Reputation: 50682

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.

Upvotes: 2

BenjaminPaul
BenjaminPaul

Reputation: 2931

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.

Upvotes: 2

Related Questions