gpt4 book ai didi

c# - 启动进程的服务不会显示 GUI C#

转载 作者:可可西里 更新时间:2023-11-01 08:33:03 24 4
gpt4 key购买 nike

嘿,我正在尝试获取服务来启动我的程序,但它没有显示 GUI。该过程开始但未显示任何内容。我已尝试启用“允许服务与桌面交互”,但仍然无法正常工作。我的程序是一个计算机锁定装置,用于阻止未经授权的用户访问计算机。我正在运行带有 64 位操作系统的 Windows 7。

这是我的服务代码:

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

FileStream fs = new FileStream(@"C:\Users\David\Documents\Visual Studio 2010\Projects\LockPCService\LockPCService\bin\Debug\ServiceLog.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 + "\n" + "\n");
m_streamWriter.Flush();
m_streamWriter.Close();
}

protected override void OnStop()
{
FileStream fs = new FileStream(@"C:\Users\David\Documents\Visual Studio 2010\Projects\LockPCService\LockPCService\bin\Debug\ServiceLog.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 + "\n"); m_streamWriter.Flush();
m_streamWriter.Close();
}

为了尝试让服务正常运行,我使用的是 notepad.exe。当我查看进程时,记事本正在运行但没有 GUI。 ServiceLog 也在那里,并且在我每次运行它时都在工作。

关于为什么这不起作用的任何想法?

谢谢。

最佳答案

This article解释了 Session 0 Isolation,其中禁止服务在 Windows Vista/7 中创建 UI。在您的服务中启动另一个进程,它在 session 0 中启动,也不会显示任何 UI。 (顺便创建了UI,只是Session 0从不显示)。 This article on CodeProject 可以帮助您从用户桌面上的服务创建流程并显示其 UI。

此外,请考虑将流对象包装在 using statement 中以便妥善处理它们。

关于c# - 启动进程的服务不会显示 GUI C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3798612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com