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
478 views
in Technique[技术] by (71.8m points)

user interface - Service starting a process wont show GUI C#

Hey, I am trying to get a service to start my program but it isn't showing the GUI. The process starts but nothing is shown. I have tried enabling 'Allow service to interact with desktop' but that still isn't working. My program is a computer locking device to stop unauthorised users from accessing the computer. I am running windows 7 with a 64 bit OS.

Here is the code for my service:

        protected override void OnStart(string[] args)
    {
        Process p = new Process();
        p.StartInfo.FileName = "notepad.exe";
        p.Start();

        FileStream fs = new FileStream(@"C:UsersDavidDocumentsVisual Studio 2010ProjectsLockPCServiceLockPCServiceinDebugServiceLog.dj",
        FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter m_streamWriter = new StreamWriter(fs);
        m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
        m_streamWriter.WriteLine(" LockPCService: Service Started " + DateTime.Now + "
" + "
");
        m_streamWriter.Flush();
        m_streamWriter.Close();
    }

    protected override void OnStop()
    {
        FileStream fs = new FileStream(@"C:UsersDavidDocumentsVisual Studio 2010ProjectsLockPCServiceLockPCServiceinDebugServiceLog.dj",
        FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter m_streamWriter = new StreamWriter(fs);
        m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
        m_streamWriter.WriteLine(" LockPCService: Service Stopped " + DateTime.Now + "
"); m_streamWriter.Flush();
        m_streamWriter.Close();
    }

To try and get the service working I am using notepad.exe. When I look at the processes notepad is running but there is no GUI. Also the ServiceLog is there and working each time I run it.

Any ideas on why this isn't working?

Thanks.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This article explains Session 0 Isolation which among other things disallows services from creating a UI in Windows Vista/7. In your service starts another process, it starts in Session 0 and also will not show any UI. (By the way, the UI is created, it's just that Session 0 is never displayed). This article on CodeProject can help you create a process from a service on the user's desktop and show its UI.

Also, please consider wrapping you stream objects in a using statement so that they are properly disposed.


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

...