3

I wrote a C# tool which is running as a standard console application. At the end of the program I used Console.Read() to prevent the window to close. This works fine for all my colleagues PC except one. He never saw my application. It did all the work to do but it closes afterwards. All PC run WinXP. Do you have any idea?

I implemented a try-catch-finally where the finally includes nothing but Console.Read().

EDIT: I added some code

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();
}
Tim M.
  • 53,671
  • 14
  • 120
  • 163
theknut
  • 2,533
  • 5
  • 26
  • 41
  • I would try to log the key recieved on last Console.Read(..), to see what key pressed on that PC, if this is possible naturally. – Tigran Sep 17 '12 at 07:18
  • I provided some code. @Maarten As I mentioned, I already use a try-catch-finally. – theknut Sep 17 '12 at 07:29
  • Have you tried running this application from an open Command prompt window on the machine that it fails on? Might shed some more light on its execution path. – Dutts Sep 17 '12 at 07:39
  • Is the problem reproduced consistently on the computer where it did not work ? And does it always work ok on all the other computers ? – Adrian Fâciu Sep 17 '12 at 07:42
  • Maybe the exception is not caught. Try something from this answer: http://stackoverflow.com/questions/406385/handling-unhandled-exceptions-problem#406473 – Maarten Sep 17 '12 at 18:16
  • I wrote a batch executing the program and than calling PAUSE. But it closed again. In windows task scheduler I saw that the exit code was 1. I can't believe I don't get any logfile and that the window will close automatically... – theknut Sep 18 '12 at 09:06
  • Have you tried placing the console.Read after the finally block? – Joshua Van Hoesen Sep 18 '12 at 17:03

1 Answers1

0

Are you using a DLL imported method (or external library) at all in any section of your CopyToolBase class?

If you are then it could be a corrupted state exception being thrown somewhere in the code, which will not be caught by a normal try/catch block, so there might be an exception, its just that the managed code can't handle it and has no choice but to terminate the process.

Here is a link explaining the situation:

I know its a long shot, but thought I'd just cover all the bases

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82