0
votes

If I want to run a simple program in the post-build events of a C# project in Visual Studio 2012, Visual Studio fails with the error "Exit from Command c:\test.exe with code -532462766".

The program 'test.exe' is straightforward:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("ciao");
        Console.ReadKey();
    }
}

But, if I remove the 'ReadKey()', it runs without errors. Can someone explain me what's wrong?

1
Are you sure this error doesn't raise somewhere else?Soner Gönül
I think yes, 'cause if I run C:\test.exe from elsewhere it runs without errors.rPulvi
Why do you want to readKey in a postbuild event? You cannot interact with the user when running a postbuild eventSuresh Kumar Veluswamy
For example because I want to read a message in the console. But, anyway..forgetting the "why I should want to read a key", let's focus on "why VS fails"rPulvi
Then can you see if this post is helpful stackoverflow.com/questions/19320266/…Suresh Kumar Veluswamy

1 Answers

4
votes

The exit code is 0xe0434352, the "CCR" exception that is the unmanaged exception that triggers managed exception handling in a .NET program. You don't have a try/catch in your code and no event handler for AppDomain.CurrentDomain.UnhandledException so you have nothing to look at but the "it crashed" exit code.

This is otherwise normal for code you run in a pre/post-build event. Such code runs in a sandbox that tries to defeat the thing you are trying to do, anything that would cause the build to stall with a "press Enter to continue" style interaction. Pretty important, nobody likes a 30 hour build on a build server to grind to a halt on something like this. You can catch the exception, you'll get an InvalidOperationException:

Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read

Which is okay advice, your program will no longer crash. But will not stop either, Console.Read/Line() was of course defeated as well. This feature is pretty solid. I've needed to do this only one time and the only way I came up with was Thread.Sleep().

When you are in Rome, act like a Roman. Redirect or write output to a file so it can be read later, after the build is completed.