C# 如果没有用户登录,如何保持windows UI运行?

C# 如果没有用户登录,如何保持windows UI运行?,c#,windows,virtual-machine,screenshot,win32com,C#,Windows,Virtual Machine,Screenshot,Win32com,我正在建立一个截屏服务。我需要能够: 1启动进程MSWord.exe 2向流程发送密钥 3.截图 while (true) { Rectangle bounds = Screen.PrimaryScreen.Bounds; using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) { using (Graphics g = Graphics.FromImage(bitmap))

我正在建立一个截屏服务。我需要能够: 1启动进程MSWord.exe 2向流程发送密钥 3.截图

while (true)
{
    Rectangle bounds = Screen.PrimaryScreen.Bounds;
    using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
        }
        bitmap.Save(string.Format("result{0}.png", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")),
                    ImageFormat.Png);
    }
    Thread.Sleep(10000);
}
如果我运行控制台应用程序并同时登录到VM,一切都会正常工作

但是如果注销,它会抛出一个错误:句柄无效。正如我所料,.CopyFromScreen不再可用


如何处理此问题?

如果没有人登录,则无法启动类似MS Word的交互式进程。这将是一个无法解决的问题。好吧,那么有没有办法打开一个会话并让它永远运行?为什么你必须注销?嗯,这是一个虚拟机,我正在远程连接它。当我关闭会话时,帐户被注销。正如@CodyGray指出的,没有会话=没有交互进程。您可以在不注销的情况下断开连接。我假设您是RDP,会话应该保持打开状态。