Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

c# - show a windows form from a window service

i am creating a window service. my requirement is to display window form from window NT service on particular interval. For testing purpose , i just want to display the form on service start:

 protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart -before form show");

            Messager_Form obj = new Messager_Form();
            obj.Show();
           // System.Diagnostics.Process.Start("calc.exe");
            eventLog1.WriteEntry("In OnStart -after form show");
           // timer1.Start();
        }

it not working. Neither form is showing nor calc process is running. i have found some links showing pop up , but most of them suggesting WCF. isn't it possible without wcf. can anybody show me the way to achieve this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Cant be done*. In later Operating Systems that won't work as Windows Services are disallowed from interacting with the Desktop - instead UI presented by Windows Services is shown in Session 0, a special logon session which is not normally visible to the end user.

What you should instead do is write a separate Windows Forms application which is always running, but not always visible (possibly have that application run at startup and have an icon in the notification area) and communicates with the Windows Service using some form of IPC

When the Windows Service wishes to display some UI to the user it sends a message to the application, which in turns shows the desired UI to the end user.

**or at least it definitely shouldn't be done*


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...