C# Console Application - cmd.exe hangs - c#

I am having issues with running a simple C# Console Application in Visual Studio 2013.
Details of my problem: I was running Console Applications successfully with the default "Press any key to continue" displaying cleanly at the end. Suddenly it started behaving differently with the following symptoms:
A new command window (cmd.exe) opening alongside my Console Application (this wasn't happening in the past)
My Console Application closing abruptly without the default clean "Press any key" message
cmd.exe hangs and I am unable to close it, even through the Task Manager -> End Process
My System Properties:
Visual Studio 2013 Ultimate
Windows 8, x64
This is my Console Application code just to show that it isn't a problem in my code:
class Program
{
static void Main(string[] args)
{
RegularTest();
}
private static void RegularTest()
{
Console.WriteLine("This is the Regular Test. It works!");
}
}

We had the exact same problem here during several weeks !
And we finally found a solution !
In our case, it was the anti-virus Avast which was corrupting the generated .exe !
The solution was to simply disable all agents while generating the release !
If you use another anti-virus, try to disable it.

Related

C# in visual studio won't work

I just bought my first Windows today (I have always used mac), but C# in my visual studio will not work. Running this:
using System;
public class Class1
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
just gives me this error and does not print anything out:
CopyWin32Resources failed with exit App3 code 500
I'm getting very frustrated can someone tell me how to get passes this?
Please choose the Console Application template in the New Project Dialog in the category Templates \ Visual C# \ Windows and copy your source code into the main method.
Then your program should start and shows a console with "Hello world".
If you add the line Console.ReadKey(); then the console window will remain open until you press a key.
With a Windows Console Application you can see output to the Console.

.net application won't run on developer machine but works on visual studio

First of all I want to apologise if this question was answered before, I've searched but I didn't find anything [maybe didn't searched correctly]
I've developed a very simple console application, which doesn't do anything else but display a random number from 2 integers.
Code:
int miNumber;
int mNumber;
bool ok = false;
try
{
miNumber = Convert.ToInt32(minNumber);
mNumber = Convert.ToInt32(maxNumber);
ok = true;
}
catch (Exception)
{
Console.WriteLine("Only digits allowed!");
Console.ReadKey();
ok = false;
}
if(ok)
{
// Generate
var x = new Random();
Console.WriteLine(string.Format("Generated number from {0} and {1} is {2}", miNumber, mNumber, x.Next(miNumber, mNumber)));
}
However, this application works on:
Developer machine if runned with visual studio [debug]
Another PC with .NET 4.0 installed.
My laptop
This application does not work on:
My own computer. If I copy test.exe in another location, it won't run.
I'm sure that I got .NET 4.0 since I run on Windows 8.1, I even tried to install it again but it says I already have it. [Anyways the Visual Studio could'nt make application in .net 4.0 if there wasn't the framework installed]
I've checked Event Viewer and I ain't see anything displayed such as error or something inputted by the system at the moment the application was run.
I tried:
Running the application from CMD [Stucks] [Screenshot added] Screenshot
Running the application from explorer.exe [Stucks and displays the loading cursor like forever]
If I close the console application displayed, the executable is still running. I tried to kill it from CMD using 'taskkill /f /im test.exe' with highest privilege available Killing however, I'm receiving an error at my second try.
The executable is still there, I can't delete or move the exe until I restart the machine.
Thanks for any help provided because I really don't get it what's the issue here.
Thanks to #HansPassant https://stackoverflow.com/users/17034/hans-passant
The problem is from Avast Antivirus.
I've disabled Avast shields and the application is working properly.
I really don't get why avast doesn't like .NET Frameworks so much but, at least is a temporary fix.
Thanks.
I'm pretty sure it could be some kind of anti virus application that's blocking your application.
Update Too late :D

No output produced for C# Console Application on 'Start without Debugging'

When attempting to run a C# console application without the debugger (ie Ctrl+F5) no output appears in the terminal.
When run with the debugger (ie just F5), program executes as expected.
I made a quick test project to make sure it wasn't just my project:
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
Console.ReadKey();
}
}
}
Run with the debugger, "Test" is output to console, without it, nothing.
I don't know what I've changed, because (my original) programme was running fine without the debugger before now
Problem was found to be Avast preventing the executable from running properly. Temporarily disabling the File System Shield acts as a workaround.
Visual Studio 14.0.23107.0 D14REL
Avast 10.4.2233 (virus definition version: 151130-0)

Stop windows command prompt from "returning" after an executable is started?

I am developing an application that writes some exception messages to the Console if they are of no use to the user. As a result if someone runs the executable by simply double clicking it, they will never see these messages.
In Visual Studio these messages show up in the output window, but in my case I am testing my application on a machine that does not a Visual Studio installation, though I still want to see if any of these messages appear.
In the past I have simply ran the executable from the command prompt and it acted as the output window in Visual Studio. Though for some reason my application, when ran from the command prompt, simple "returns" and does not show any messages.
For example I might start it like so, and it instantly returns
D:\>MyApp.exe
D:\>
I am not sure if there is a specific switch I should use (I have tried /K to no avail) when I run it, or if there is something about my application that causes it to return.
Any ideas on how I might run it via the command line so I can see the messages?
For reference here is my applications Program.cs
static class Program
{
static Mutex mutex = new Mutex(true, "{12345678-1234-1234-1234-123456789012}");
[STAThread]
static void Main()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SplashScreen.ShowSplashScreen();
Application.Run(MainForm.Instance);
mutex.ReleaseMutex();
}
else
NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_JTTMAINWINDOW, IntPtr.Zero, IntPtr.Zero);
}
}
To allow your program to output to the console, build it as a console application.
In Visual Studio, the relevant linker option is /SUBSYSTEM:Console ; or, when creating a new project, choose the "console application" template which will set the linker option automatically.
This will also mean that if you run the program from the command prompt, the command prompt will wait for the program to exit. Also, if you double-click the program rather than running it from the command prompt, a console window will automatically be created.
(There is no way to make the command prompt wait for a program but prevent the program from creating a console window if double-clicked. See this question and its answers for more information.)

Console application not closing

I'm developing a console application that is supposed to run under WinCE 6.0 and WinCE 7.0. I'm using C#, Compact Framework 2.0 for different compatibility reasons.
My application is started by an external runtime called TwinCAT (from Beckhoff). Within this application, my teammate used a function block called nt_startProcess (documentation here) that is in charge of starting my application on demand.
My problem - Two different behaviors depending on the OS :
When started manually (without TwinCAT) from a cmd line :
My application behaves properly on both systems. It means that, the applications starts, displays "Hello World" and then returns to the cmd line.
When started from TwinCAT :
a) On WinCE 6.0, I can see a cmd line opening, displaying "Hello World" and shutting itself right after. Perfect behavior to me.
b) On WinCE 7.0, I can see a cmd line opening, displaying "Hello World" but it remains open forever. This is my problem!
Code snippet :
using System;
using System.Collections.Generic;
using System.Text;
namespace MyBasicExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
}
Compilation information
In Visual Studio 2008, within the Project compilation's properties :
Plateform target : Any CPU
Additionnal note :
Please note that the computer who is running WinCE 6.0 is using a i486 processor while the one running WinCE 7.0 is using a Freescale ArmCortex process.
WinCE 6.0 :
WinCE 7.0 :
What I tried :
1) Using return 0; at the end of application.
Doesn't change anything on WinCE 7.0.
2) Using Environment.Exit(0);
Is not available in Compact Framework 2.0.
3) Using the property : IsBackground
Snippet :
// ... Same snippet as above except for the next line...
Thread.CurrentThread.IsBackground = true;
Console.WriteLine("Hello World");
// ...
4) From TwinCAT, calling a batch file (which calls my exe) instead of my exe.
Doesn't work with TwinCAT. I get an error of type "General Sub-Windows error".
5) Tested with the Compact Framework 3.5.
Same behavior.
6) Tested with another CX computer (model 2020) using Windows CE 7.0 and another processor architecture (Intel Pentium III Xeon Model A).
Same behavior.
try this code:
Environment.Exit(0);
Try this:
Tools > Options > Debugging > Automatically Close the Console When Debugging Stops
Are you putting you .exe file in Arguments property of ProcessStartInfo ?
If you must do that, I believe that you're using CMD in FileName property, so you must use /K before your .exe name.
Or just put in FileName the .exe path.
You can clarify a lot if you put the code that calls your application.
Try calling Application.Exit
This works in windowed applications, and may force the console window to close.
I had exactly the same problem. Running console app on Beckhoff PLC which never closed.
Instead of creating Console app I created Windows App.
My code stayed the same as for console app. I just commented out:
// Application.Run(new Form1());
Seems now codes run without opening a form.

Categories