控制台窗口在使用Console.Read()后关闭

3
我写了一个C#工具,它作为标准的控制台应用程序运行。在程序末尾,我使用了Console.Read()来防止窗口关闭。这对我的所有同事的电脑都有效,除了一台。他从未看到过我的应用程序。它完成了所有要做的工作,但之后会关闭。所有电脑都运行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(..) 接收到的按键,以查看在该计算机上按下了哪个键,如果自然可行的话。 - Tigran
我提供了一些代码。 @Maarten 如我所述,我已经使用了try-catch-finally。 - theknut
你尝试过在出现问题的机器上从打开的命令提示符窗口运行此应用程序吗?这可能会更清楚地显示其执行路径。 - Dutts
可能异常没有被捕获。尝试一下这个答案中的方法:https://dev59.com/y3RC5IYBdhLWcg3wG9To - Maarten
我写了一个批处理程序来执行程序,然后调用PAUSE。但它又关闭了。在Windows任务计划程序中,我看到退出代码为1。我无法相信我没有得到任何日志文件,窗口也会自动关闭... - theknut
显示剩余2条评论
1个回答

0
你在你的CopyToolBase类的任何部分中是否使用了导入方法(或外部库)的DLL?
如果是这样的话,代码中可能会抛出损坏状态异常,在正常的try / catch块中不能捕获,因此可能会出现异常,只是托管代码无法处理它并且别无选择而终止进程。
这里有一个 link 解释了这种情况:
我知道这是一个很长的路程,但我想涵盖所有基础。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接