【问题标题】:Console Window Closes Despite Using Console.Read()尽管使用了 Console.Read(),但控制台窗口关闭
【发布时间】:2012-09-17 07:03:11
【问题描述】:

我编写了一个作为标准控制台应用程序运行的 C# 工具。在程序结束时,我使用Console.Read() 来防止窗口关闭。这适用于我所有同事的 PC,除了一台。他从来没有看过我的申请。它完成了所有工作,但之后关闭。所有 PC 都运行 WinXP。你有什么想法吗?

我实现了一个 try-catch-finally,其中 finally 只包含 Console.Read()

编辑:我添加了一些代码

Console.SetWindowSize(125, 40);
CopyToolBase ctb = null;
try
{
    DateTime startTime = DateTime.Now;
    TimeSpan duration;

    ctb = CopyToolBase.GetInstance(Defines.configPath);
    if (null == ctb)
    {
        throw new KopiertoolException();
    }

    if (null == ctb.GetNewestVersion())
    {
        throw new KopiertoolException();
    }

    if (!ctb.CheckCopy())
    {
        throw new KopiertoolException();
    }

    if (!ctb.CopyAndUnzip())
    {
        throw new KopiertoolException();
    }

    duration = DateTime.Now.Subtract(startTime);

    ctb.PrintSuccess("xxxxxxxx");
    ctb.PrintInfo("Gesamtdauer: " + ((duration.Hours == 0) ? "" : string.Format("{0:00} Std ", duration.Hours)) + string.Format("{0:00} Min {1:00} Sek", duration.Minutes, duration.Seconds));

    startTime = DateTime.Now;

    if (!ctb.StartTask())
    {
        throw new KopiertoolException();
    }

    duration = DateTime.Now.Subtract(startTime);

    if (duration.Minutes > 1)
    {
        ctb.PrintInfo("Dauer: " + ((duration.Hours == 0) ? "" : string.Format("{0:00} Std ", duration.Hours)) + string.Format("{0:00} Min {1:00} Sek", duration.Minutes, duration.Seconds));
    }
}
catch (KopiertoolException)
{
    ctb.WriteToLog();
}
catch (Exception ex)
{
    if (ctb == null)
    {
        Console.WriteLine("xxxxxxxxx");
        Console.WriteLine(ex.ToString());
    }
    else
    {
        ctb.PrintError("xxxxxxxxx");
        ctb.PrintError(ex.ToString());
        ctb.WriteToLog();
    }
}            
finally
{
    Console.Read();
}

【问题讨论】:

  • 我会尝试记录在最后一个 Console.Read(..) 上收到的密钥,看看那台 PC 上按下了什么键,如果这很自然的话。
  • 我提供了一些代码。 @Maarten 正如我所提到的,我已经使用了 try-catch-finally。
  • 您是否尝试过从它失败的机器上打开的命令提示符窗口运行此应用程序?可能会对其执行路径有所了解。
  • 问题是否在无法正常工作的计算机上始终如一地重现?它在所有其他计算机上总是可以正常工作吗?
  • 可能没有捕获到异常。试试这个答案:stackoverflow.com/questions/406385/…

标签: c# console-application


【解决方案1】:

您是否在 CopyToolBase 类的任何部分都使用了 DLL 导入方法(或外部库)?

如果是,那么它可能是代码中某处抛出的损坏状态异常,不会被正常的 try/catch 块捕获,因此可能存在异常,只是托管代码不能处理它,别无选择,只能终止进程。

这里是link 解释情况:

我知道这是一个远射,但我想我会覆盖所有的基地

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 2021-10-10
    • 2014-04-11
    • 2017-03-29
    • 2012-10-14
    相关资源
    最近更新 更多