避免“按任意键继续".从Visual Studio运行控制台应用程序时 [英] Avoiding "Press any key to continue" when running console application from Visual Studio

查看:436
本文介绍了避免“按任意键继续".从Visual Studio运行控制台应用程序时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过不调试开始"( Ctrl + F5 )在Visual Studio中运行控制台应用程序时,控制台在运行结束时保持打开状态,要求

When running a console application in Visual Studio via "Start without Debugging" (Ctrl+F5), the console remains open at the end of the run asking to

按任意键继续. .

Press any key to continue . . .

因此需要激活窗口并按下键.有时这不合适.

thus requiring to activate the window and hit a key. Sometimes this is not appropriate.

这很重要: 在我编写json序列化代码的那一刻,我的工作流程如下:

Why this matters: At the very moment I write json serialisation code, my workflow goes like this:

  • 调整c#代码
  • 运行一个将文件out.json写入的控制台应用程序
  • 在浏览器中使用json查看器查看out.json

一次又一次地执行此操作,无需调试任何东西,只需修剪序列化并检查输出是否良好即可.

do this again and again, no need to debug anything, just trimming serialisation and check output is good.

就是这样的工作流程,其中按任意..."的行为受阻,因为它需要步骤

It is workflows like this, where the "press any ..." behavior is hindering as it requires the steps

  • 激活控制台窗口
  • 按键
  • activate the console window
  • press key .

没有答案:

  • 在VS之外的单独控制台中启动应用程序不是答案.
  • 说,您不需要这个.

推荐答案

我很确定您不会影响或更改此行为.

I'm pretty sure that you cannot affect or change this behavior.

正如您提到的,它与应用程序本身无关,因为双击EXE时它与应用程序无关.只有在未连接调试器的情况下从Visual Studio中运行应用程序时,您才会看到这种效果.

As you mention, it has nothing to do with your application itself, because it doesn't do it when you double-click on the EXE. You only see this effect when you run the app from within Visual Studio without the debugger attached.

大概是,当您调用 Ctrl + F5 时,Visual Studio以一种导致控制台窗口保持打开状态的特定方式运行您的应用程序.我可以想到两种方式:

Presumably, when you invoke Ctrl+F5, Visual Studio is running your app in a particular way that causes the console window to remain open. I can think of two ways it might be doing it:

%COMSPEC% /k "C:\Path\To\YourApplication.exe"

%COMSPEC% /c ""C:\Path\To\YourApplication.exe" & pause"

使用这两种方法,您看到的暂停行为都直接包含在用于启动应用程序的命令中,因此在您的应用程序外部.因此,除非您有权访问Visual Studio源代码,否则您将不会对其进行更改.从您的应用程序中调用exit函数不会产生任何效果,因为在出​​现该消息时,您的应用程序已经退出.

With either of these, the pausing behavior you're seeing is baked right into the command used to launch your app and is therefore external to your application. So unless you have access to the Visual Studio sources, you're not going to change it. Calling an exit function from your app won't have any effect because your app has already quit by the time that message appears.

当然,除了好奇心之外,我看不出它为何如此重要.在连接调试器的情况下启动应用程序时不会发生这种情况,这是从IDE启动应用程序时99%的时间.而且由于您没有将Visual Studio与应用程序一起发布,因此您的用户 将在VS之外启动应用程序.

Of course, I can't see why it really matters, aside from an issue of curiosity. This doesn't happen when you start the app with the debugger attached, which is what you'll be doing 99% of the time when you launch the app from the IDE. And since you don't ship Visual Studio along with your app, your users are going to be starting the app outside of VS.

为响应对您的问题所做的更新,最好的解决方案是更改您的应用程序,使其不是控制台应用程序.此行为不会影响标准Windows应用程序.当他们关门时,他们永远关门.

In response to the updates made to your question, the best solution would be to change your app so that it is not a console application. This behavior doesn't affect standard Windows applications; when they get closed, they close for good.

如果您不需要控制台窗口上的任何输出,则此操作非常简单:只需. Windows窗体应用程序将正常运行.如果您不显示窗口(又名窗体),则会自动创建.这是常规Windows应用程序和控制台应用程序之间的区别,无论您是否需要,它们都会始终创建一个控制台窗口.

If you do not require any output on the console window, then this is very simple to do: just change the "Application type" in your project's properties. A Windows Forms application will work just fine. If you do not display a window (aka form), one will not be automatically created. This is the difference between regular Windows applications and console applications, which always create a console window, whether you need one or not.

如果要做需要在控制台窗口上显示输出,则有两个选项:

If you do need to display output on the console window, you have a couple of options:

  1. 使用ListBox或ListView控件创建并使用一个简单的表单.您通常会输出到控制台的每一行,都将作为新项添加到列表控件中.如果您不使用控制台的任何高级"功能,则此方法效果很好.
  2. P/调用并调用 AllocConsole 函数以创建Windows应用程序可以使用的控制台.您不需要为此填写表格.

这篇关于避免“按任意键继续".从Visual Studio运行控制台应用程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆