简体   繁体   English

如何暂停C#控制台应用程序(仅在由用户运行时)

[英]How can I pause a C# Console application (only when being run by a user)

How can I pause a C# console application only while being run by a user? 如何在用户运行时暂停C#控制台应用程序?

I've searched similar issues on stack overflow which recommend learning to debug properly, using readline, etc, but none of them cover the instance where your process might be being run by an automated system that needs to continue straight past (ie not pause, to let user read screen). 我在堆栈溢出中搜索了类似的问题,这些问题建议学习如何正确调试,使用readline等,但是这些问题都没有涉及您的流程可能由需要继续过去(即不暂停,让用户阅读屏幕)。

How can I determine whether the console is being run interactively or by a script? 如何确定控制台是交互式运行还是通过脚本运行?

If this were bash on a linux system, I could do something like 如果这是Linux系统上的bash,我可以做类似的事情

https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html

Using some command line argument would be the easiest solution. 使用某些命令行参数将是最简单的解决方案。 For example if program is run with command line parameter "-automatic" it could just run without stopping. 例如,如果程序使用命令行参数“ -automatic”运行,则可以不停止而直接运行。 Without such parameter program would run in interactive mode and would expect user input. 如果没有这样的参数,程序将以交互模式运行,并且需要用户输入。

It is pretty standard approach used by many command line utilities. 这是许多命令行实用程序使用的非常标准的方法。

You need to check if someone is logged in as the interactive user, but this is mostly for usage with services. 您需要检查是否有人以交互用户身份登录,但这主要是用于服务。 Some "automated" systems since you mention such a term may be faking a logged-in user. 自从您提到这样的术语以来,某些“自动化”系统可能会伪造已登录的用户。

There are some suggestions available at: Check if no user is currently logged on to Windows 在以下位置有一些建议: 检查当前是否没有用户登录Windows。

Probably the easiest way (especially if you don't want to rely on WMI API) is to enumerate user sessions via P/Invoke call, as shown here: http://www.pinvoke.net/default.aspx/wtsapi32/WTSEnumerateSessions.html 可能最简单的方法(尤其是如果您不想依赖WMI API)是通过P / Invoke调用枚举用户会话,如下所示: http : //www.pinvoke.net/default.aspx/wtsapi32/WTSEnumerateSessions html的

Console.OpenStandardInput(1) will return Stream.Null if the console Application is not being ran interactively. 如果未交互运行控制台应用程序,则Console.OpenStandardInput(1)将返回Stream.Null。

    private static void pause()
    {
        if (Console.OpenStandardInput(1) != Stream.Null)
        {
            Console.Out.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户按下escape时,如何“暂停”控制台应用程序? - How do I “Pause” a console application when the user presses escape? C# Windows 控制台应用程序如何判断它是否以交互方式运行 - How can a C# Windows Console application tell if it is run interactively 如何在c#中的控制台应用程序中在后台运行程序 - How can I make a program run in the background from a console application in c# 自动暂停C#中的控制台应用程序 - Automatically pause the console application in c# 我怎么能放一个规则,只允许您在C#的控制台应用程序中输入数字 - how can i put a rule that will only let you enter a number in a console application on C# C#在另一个控制台应用程序启动时暂停一个控制台应用程序 - C# Pause one console application when other console application starts 在 C# DLL 文件中作为进程运行时,如何获取控制台应用程序输出? - How can I obtain console application output when running it as a process in a C# DLL file? 如何在C#控制台应用程序中使用配置替换 - How Can I Use Configuration Replacements for C# Console Application 我如何在 c# 控制台应用程序中执行 CMD 命令? - how i can execute CMD command in c# console application? 如何在控制台应用程序C#中访问SolidBrush - How Can I Access To SolidBrush In Console Application C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM