I have an Solution that has been working without issues for years. The Program.cs of the main Startup project (a WinForms app) has not been changed for a couple of years. But recently I have had issues running it in Debug. I thought I had solved the issue by cleaning the projects and deleting the bin and obj files. However, today I have only managed to run and debug the project twice. (The 2nd time was after a Repair of Visual Studio (But it has since failed again).
When trying to run in debug or release, the only information I get is:
The program ‘[25648] MyApp.exe’ has exited with code 0 (0x0).
How can I debug the issue further?
Even the 1st line of the class or the 1st line of the Main() method is not reached (using a breakpoint):
private static string test1 = "STOP HERE";
mutex = new Mutex(true, AppName, out bool createdNew);
Here is the beginning of my Program.sc file (Unchanged since Feb 2023 other than the line I added with "STOP HERE" for debug purposes).
static class Program
{
private static string test1 = "STOP HERE";
const string AppName = "MyApp";
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static Mutex mutex = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
mutex = new Mutex(true, AppName, out bool createdNew);
if (!createdNew)
{
MessageBox.Show("MyApp is already running.", KwikConstants.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //app is already running! Exiting the application
Application.Exit();
}
}
}
It is written in c# using VS 2022 v17.12.3
.Net 8.5 Framework
There are no PreBuild scripts.
I have tried in x64, x86 and Any CPU.
I have updated all NuGet packages.
The full solution Builds with no errors.
My PC OS is Windows 11 23H2
I have tried to add the .exe to my Antivirus Exclusion and even deactivated AV while testing.
Any suggestions on how to debug the issue would be greatly appreciated.
2
Answers
Your code is bombing at one of three possibilities:
Solution:
Create a new startup (ProgramName), that is clean, and has a basic Console Write. Create a copy of the production folder, copy over your EXE and run it.
The code is doing exactly what you told it to do.
That is entirely what you would expect from that code.
You need to add some code to an
else
thatnew
s up the relevant form and then shows it. That isn’t magic – it won’t do it automatically without you writing that code in theMain
.As to how you got into this state – my guess is that in the past you had the form as the startup object. Then you realised you only want a single instance running – so you copied and pasted some code to do that, and changed the startup object to
Main
. But when you do that you need to write code inMain
to actually show the form. That won’t happen automatically.