如何拥有控制台的程序执行时间? [英] How to own console for time of program execution?

查看:124
本文介绍了如何拥有控制台的程序执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序,它在控制台或GUI模式下工作,这取决于执行参数。我已经设法写下面的示例代码:

I'm trying to write a program, that works in console or GUI mode, depending on execution parameters. I've managed to write following sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace wfSketchbook
{
    static class Program
    {
        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AttachConsole(int processId);

        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AllocConsole();

        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool FreeConsole();

        private const int ATTACH_PARENT_PROCESS = -1;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                if (!AttachConsole(ATTACH_PARENT_PROCESS))
                    AllocConsole();
                Console.WriteLine("Welcome to console!");
                Console.ReadKey();
                FreeConsole();
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
}

一般工作,但是当程序从系统命令行调用时,cmd似乎没有意识到,该程序在控制台模式下工作并立即退出:

It generally works, however when program is called from the system command-line, the cmd seems not to be aware, that program works in console mode and exits immediately:

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>wfSketchbook.exe test

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>Welcome to console!

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>

我希望得到以下输出:

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>wfSketchbook.exe test

Welcome to console!

d:\Dokumenty\Dev\C#\Projekty\Win32\Sketchbook\wfSketchbook\bin\Debug>

如何解决这个问题?

推荐答案

没有任何可靠的方法使Windows应用程序成为控制台和GUI。您的程序是一个Windows应用程序 - 因此Windows会在控制台窗口之外启动您的程序 - 当您的程序启动时,您没有连接到控制台窗口。

There isn't any reliable way to make a Windows application a console and a GUI. Your program is a Windows application - so Windows launches you outside of the console window - when your program starts you aren't attached to the console window.

项目输出到项目属性中的控制台应用程序。但是你总是会得到一个控制台窗口。 Windows可能会看到您的应用程序已标记为控制台应用程序,并在运行之前创建控制台。

You could change your project output to a Console application in the project's properties. But then you would always get a console window. Windows could see that your application was marked as a console application and create a console even before you ran.

请参阅此 blog post 了解更多信息和一些工作链接。

See this blog post for more information and links to some work arounds.

这篇关于如何拥有控制台的程序执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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