2012-09-17 61 views
3

我写了一个C#工具,作为标准控制台应用程序运行。在程序结束时,我使用了Console.Read()来防止窗口关闭。这对所有我的同事个人电脑除了一个以外都可以。他从未看到我的申请。它完成了所有的工作,但之后关闭。所有PC运行WinXP。你有什么主意吗?控制台窗口关闭,尽管使用Console.Read()

我实现了一个try-catch-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(); 
} 
+2

一些代码将是有益的。 –

+0

我会尝试记录上次Console.Read(..)上收到的密钥,以查看在该PC上按下的按键,如果这是可能的话。 – Tigran

+0

也许有例外吗? – Maarten

回答

0

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

如果你是那么它可能是一个损坏的状态异常被抛出代码中的某处,这将不会被正常的try/catch块捕获,所以可能会有一个异常,只是托管代码可以'没有办法处理它,只好终止这个过程。

这里是一个link说明情况:

我知道它是一个长镜头,但认为我只是涵盖所有的基础

相关问题