简体   繁体   中英

C# - Visual Studio crashes at runtime, running the compiled program externally works fine

I'm trying to pick up C# (I'm somewhat comfortable with C++, but Qt goes way over my head) but I'm running into a show-stopping issue with MSV.

Namely, sometimes when I build & run my code the resulting program will crash, where if I build it and run it outside of the IDE it works as intended.

I've come across this issue a few different times on a few different programs, and I don't always get the same error code, but the result is always the same - the console window appears but nothing is written to it; it hangs for a second, and then 'vshost32.exe has stopped working' pops up. Following the error message's debug option does nothing, as it says it isn't configured.

Here's an example of a complete program that's giving me the issue:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int number1, number2, number3;

            Console.WriteLine("Please enter a number:");
            number1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Please enter another number:");
            number2 = int.Parse(Console.ReadLine());

            Console.WriteLine("Calculating...");
            number3 = number1 * number2;

            switch (number3)
            {
                case 0:
                    Console.WriteLine("The result is equal to zero.");
                    break;
                case 1:
                    Console.WriteLine("The result is equal to one.");
                    break;
                default:
                    Console.WriteLine("The result is not equal to zero or one.");
                    break;
            }

            Console.ReadLine();
        }
    }
}

Here's where it gets stranger.

Commenting out the case statements removes the problem entirely. It compiles and runs properly from the editor, and runs without an issue. However, I can't see any reason why the two case statements would be causing an issue. For the record, this program exits with code "-1073741819 (0xc0000005) Access violation".

Just to humor the IDE and see if it really was a problem in my code, I ran the first example program from this page: http://csharp.net-tutorials.com/basics/switch-statement/ And the same thing happened . Hang, crash, 0xc0000005 Access violation. And, just like before, when I manually run the .EXE file outside of the IDE it works just fine.

I reinstalled .NET Framework a few times to see if that was the issue and it wasn't - I even tried rolling back from 4.5.3 to 4.5, that just broke it even more. Right now I'm totally stumped, because I can't really do much of anything as long as I have to leave the IDE to run my code. I'm running Windows 10 (b10041) and am using Visual Studio 2013.

In the project there is a bin folder with an exe in the debug and release folder. Make sure when you compile the date of the exe is getting updated. You are probably are not using the latest exe file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM