启动进程的服务不会显示 GUI C# [英] Service starting a process wont show GUI C#
问题描述
嘿,我正在尝试获取一个服务来启动我的程序,但它没有显示 GUI.该过程开始,但没有显示任何内容.我尝试启用允许服务与桌面交互",但仍然无效.我的程序是一个计算机锁定设备,用于阻止未经授权的用户访问计算机.我在 64 位操作系统上运行 Windows 7.
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.
这是我的服务的代码:
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();
}
为了尝试使服务正常工作,我正在使用 notepad.exe.当我查看进程记事本正在运行但没有 GUI 时.每次我运行它时,ServiceLog 都在那里工作.
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?
谢谢.
推荐答案
本文 解释了 Session 0 Isolation
,其中包括禁止服务在 Windows Vista/7 中创建 UI.在您的服务启动另一个进程时,它在会话 0 中启动并且也不会显示任何 UI.(顺便说一下,创建了 UI,只是 Session 0 永远不会显示).这篇关于 CodeProject 的文章可以帮助您从用户的桌面并显示其 UI.
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.
另外,请考虑将流对象包装在 using代码> 声明
以便正确处理它们.
Also, please consider wrapping you stream objects in a using
statement so that they are properly disposed.
这篇关于启动进程的服务不会显示 GUI C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!