如果从命令行启动,则输出到命令行 [英] Output to command-line if started from command line

查看:237
本文介绍了如果从命令行启动,则输出到命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,可以作为标准的WinForms应用程序或在无人值守模式从命令行启动。该应用程序使用VS 2k5标准的WinForms模板构建。

I'm writing an application that can be started either as a standard WinForms app or in unattended mode from the command-line. The application was built using the VS 2k5 standard WinForms template.

当从命令行执行应用程序时,我希望它输出可以被脚本执行应用程序。当我直接从Console.WriteLine()这样做时,输出不出现,虽然它可以通过管道捕获到一个文件。

When the application is executed from the command-line, I want it to output information that can be captured by the script executing the application. When I do this directly from Console.WriteLine(), the output does not appear, although it can be captured by piping to a file.

另一方面,我可以强制应用程序通过对AllocConsole()从kernel32执行P / Invoke来弹出第二个控制台。这不是我想要的,虽然。我想让输出出现在应用程序从同一个窗口中调用。

On the other hand, I can force the application to pop up a second console by doing a P/Invoke on AllocConsole() from kernel32. This is not what I want, though. I want the output to appear in the same window the application was called from.

这是显式代码,允许我从命令行弹出控制台: / p>

This is the salient code that allows me to pop up a console from the command line:

<STAThread()> Public Shared Sub Main()

    If My.Application.CommandLineArgs.Count = 0 Then
        Dim frm As New ISECMMParamUtilForm()
        frm.ShowDialog()
    Else
        Try
            ConsoleControl.AllocConsole()
            Dim exMan As New UnattendedExecutionManager(ConvertArgs())
            IsInConsoleMode = True
            OutputMessage("Application started.")
            If Not exMan.SetSettings() Then
                OutputMessage("Execution failed.")
            End If
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        Finally
            ConsoleControl.FreeConsole()
        End Try

    End If

End Sub

Public Shared Sub OutputMessage(ByVal msg As String, Optional ByVal isError As Boolean = False)
    Trace.WriteLine(msg)
    If IsInConsoleMode Then
        Console.WriteLine(msg)
    End If

    If isError Then
        EventLog.WriteEntry("ISE CMM Param Util", msg, EventLogEntryType.Error)
    Else
        EventLog.WriteEntry("ISE CMM Param Util", msg, EventLogEntryType.Information)
    End If

End Sub


推荐答案

更新1:

如Michael Burr所述, Raymond Chen 最近发布了一篇关于此的短文。我很高兴看到我的猜测并不完全错误。

As said in Michael Burr answer, Raymond Chen recently posted a short article about this. I am happy to see that my guess was not totally wrong.

更新0:

免责声明:这个答案主要是投机。我发布它只是因为足够的时间已经过去,确定没有多少人有什么看起来像一个根本问题的答案。

Disclaimer: This "answer" is mostly speculation. I post it only because enough time has passed to establish that not many people have the answer to what look like a fundamental question.

我认为决定如果应用程序是gui或控制台是在编译时,而不是在运行时。所以如果你编译你的应用程序作为gui应用程序,即使你不显示gui,它仍然是一个gui应用程序,没有控制台。如果你选择编译它作为控制台应用程序,那么至少你会有一个控制台窗口闪烁后,移动到gui模式。我不知道是否可能在托管代码。

I think that the "decision" if the application is gui or console is made at compile time and not at runtime. So if you compile your application as gui application, even if you don't display the gui, its still a gui application and doesn't have console. If you choose to compile it as console application then at minimum you will have a console windows flashing before moving to gui "mode". And I don't know if it is possible in managed code.

问题是根本的,我想,因为控制台应用程序必须采取控制控制台应用程序。并且它必须在子应用程序的代码运行之前这样做。

The problem is fundamental, I think, Because a console application has to take "control" of the calling console application. And it has to do so before the code of the child application is running.

这篇关于如果从命令行启动,则输出到命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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