简体   繁体   English

Console.Write在Win Forms应用程序中不起作用

[英]Console.Write Not Working In Win Forms App

I created a VB.NET Windows Forms Application in Visual Studio 2008. When I run my program from the command-line, I get no output (only the next prompt). 我在Visual Studio 2008中创建了VB.NET Windows窗体应用程序。从命令行运行程序时,没有任何输出(只有下一个提示)。

What am I doing wrong? 我究竟做错了什么?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Debug.Write("Foo")
    Debug.Flush()
    Console.WriteLine("foo")
    Console.Beep(800, 100) 'confirm this function is called'
    Me.Close()
End Sub

EDIT: Can a program have a form and a console? 编辑:程序可以有一个窗体和一个控制台吗?

EDIT2: Ho's answer works. 编辑2:何的答案有效。 However, the output appears on the next command-line prompt. 但是,输出出现在下一个命令行提示符下。 Can a Winforms application tell the command-line to wait until it's finished instead of immediately returning? Winforms应用程序可以告诉命令行等待它完成而不是立即返回吗?

Tested similar code with a C# .NET Windows Form Application. 使用C#.NET Windows窗体应用程序测试了类似的代码。 Outputs and beeps nicely within Visual Studio but only beeps when run at the command line. 在Visual Studio中可以很好地输出和发出蜂鸣声,但是只有在命令行运行时才发出蜂鸣声。

If I change the Output Type to Console Application under the Application tab for the Project properties, I get to use both the form and console :) 如果在“项目”属性的“ 应用程序”选项卡下将“ 输出类型”更改为“ 控制台应用 程序” ,则可以同时使用表单和控制台:)

You can run your app from the console using myApp.exe|MORE 您可以使用myApp.exe|MORE从控制台运行应用程序myApp.exe|MORE
This way the console will show Console.WriteLine() calls coming from the app and will wait until the app exits. 这样,控制台将显示来自应用程序的Console.WriteLine()调用,并将等待直到应用程序退出。
Please excuse my bad english. 请原谅我英语不好。

The others are correct in saying that you need to run your app as a console app. 其他人的正确说法是您需要将应用程序作为控制台应用程序运行。 To your question of whether you can have both a console and a GUI: yes. 关于是否可以同时拥有控制台和GUI的问题:是的。 Simply add a reference to System.Windows.Forms to your project, and for the app's Main method, include this code: 只需在项目中添加对System.Windows.Forms的引用,对于应用程序的Main方法,请包含以下代码:

' here instantiate whatever form you want to be your main form '
Dim f As New Form1

' this will start the GUI loop, which will not progress past '
' this point until your form is closed '
System.Windows.Forms.Application.Run(f)

Or if you already have a WinForms application you can attach a Console to it using the AttachConsole API. 或者,如果您已经拥有WinForms应用程序,则可以使用AttachConsole API将控制台附加到该应用程序。

using System.Runtime.InteropServices;  

... ...

[DllImport("kernel32.dll")] static extern bool AttachConsole(int dwProcessId);
private const int ATTACH_PARENT_PROCESS = -1;  

... ...

AttachConsole(ATTACH_PARENT_PROCESS);

(Formatted as code) (格式化为代码)

尝试使用“控制台应用程序”模板创建一个新项目。

The solution is: 解决方案是:

Declare Function AttachConsole Lib "kernel32.dll" (ByVal dwProcessId As Int32) As Boolean
Declare Function FreeConsole Lib "kernel32.dll" () As Boolean

[code.....] [码.....]

when needed the console output you just call AttachConsole(-1) but remember to do a FreeConsole() at the end of your program. 需要控制台输出时,只需调用AttachConsole(-1),但要记住在程序末尾执行FreeConsole()。

the only problem with this solution is that you will read something like this: 此解决方案的唯一问题是,您将阅读以下内容:

C:>yourapp.exe C:> yourapp.exe
C:>Hello World! C:>你好,世界!

That's because a forms application runs as a child of the command prompt so the prompt returns immediately after you type the app name.. 这是因为表单应用程序作为命令提示符的子级运行,因此在键入应用程序名称后,提示符将立即返回。

In a console application the command prompt returns after the program exits. 在控制台应用程序中,程序退出后,命令提示符将返回。

I am still trying to find a way to have the same behaviour (sync run) as in a console application. 我仍在尝试找到一种与控制台应用程序具有相同行为(同步运行)的方法。

您必须创建一个命令行/控制台应用程序而不是Windows窗体应用程序才能使用控制台。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM