Unable to run C# in Visual Studio Code - c#

Whenever I attempt to run C# in VS Code, the program asks for the environment. When I pick ".NET 5+ and .NET core" all that happens is a settings.json file gets opened up as another tab. The second environment option is ".NET Framework 4.x (Windows Only)" all that happens then is that a flash of the code running bar pops up.
However it can run Java, but I do not want to do Java. It could be an user error, I wouldn't be surprised if I just couldn't find how to run code.

The problem is that the code runs and exits immediately
You should add Console.ReadKey() at the end of the Main method like this:
using System;
class Program
{
static void Main(string[] args)
{
// Your Code here
Console.ReadKey(); // At the end of the Main method
}
}

Related

How to register my visual studio extension to run events?

I have a visual studio extension and I want it to run another executable before the program itself is running without debugger (when the program runs with a certain configuration).
how can I do that?
I managed to do that when the program is being debugged - I used DTE.Events.DebuggerEvents interface. is there a similar event for running the program without debugger?
private static void EventsRegistering()
{
DTE2 visualStudioInstance;
debuggerEvents = visualStudioInstance.Events.DebuggerEvents;
debuggerEvents.OnEnterRunMode += DebuggerEvents_OnEnterRunMode;
}
Just to be clear - I don't want it to run with every build of the program, just for every run.
You can subscribe to DTE.Events.CommandEvents.OnBeforeExecute and detect when a command name is Debug.StartWithoutDebugging. See for example Monitor commands execution.

How to use Console.ReadLine() in a project using MonoDevelop under Linux

I have the following sample project and I use MonoDevelop in OpenSuse.
I can build the project, but when I execute it, it does not show any console where I can enter the string. I am aware about people indicating that we should set the Project options to "Run on External Console". That option is enabled, but there is no console appearing when I execute the program under MonoDevelop. Do we have to open a console separately and attach to process, or is there another trick?
using System;
namespace ConsoleReadLine
{
class MainClass
{
public static void Main (string[] args)
{
string s = Console.ReadLine ();
Console.WriteLine (s);
Console.ReadLine ();
}
}
}
Currently (Version 7.8.4 (build 2)), this is still an issue on Linux.
To address it, you need to:
Right-click on the solution (and not the project), and select "Options"
In "Run", select "Default" under "Configurations", tick "Run on external console"
Click "OK", and that's it!
I don't know if there is any way to change this behavior system-wide (that is, for all solutions), but would be interested in knowing.

C# Console Application build but not launch application

I have a simple Console Application write with C-Sharp language (Visual Studio 2013):
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
When I press F5 or click Start button, my project was built, but not launch.
Sometime, Ouput windows says:
Error 12 Could not copy "obj\Debug\HelloWorld.exe" to "bin\Debug\HelloWorld.exe". Exceeded retry count of 10. Failed.
Error 13 Unable to copy file "obj\Debug\HelloWorld.exe" to "bin\Debug\HelloWorld.exe". The process cannot access the file 'bin\Debug\HelloWorld.exe' because it is being used by another process.
but when I write Windows Form Application, my project was built and launch normally ???
Why? and How to solve this problem ?
This is most likely due to windows keeping the process open. Your only option is to try and kill all processes of your app in task manager->processes.
The next thing to try is to simply change the build from debug to release, this should build another executable in the release folder as opposed to debug. By no means is this a silver bullet but, hopefully a sufficient workaround.
Before you try the release build, attempt to try and fix the problem. I've seen windows moan at just having my debug folder open and my exe selected because I suspect the thumbnail was being displayed thus being "used" in windows etc.
I found reason and solve for my problem.
I tried to restart Application Experience Service and the problem was solved.
Please restart yor Visual Studio and then try again. It will work and delete your bin/debug folder again.
I have faced with this issue.
That happeneds when an instance of software is running whether by visual studio or by your self
solution :
you should find your application name in the processes of task manager
your's is :
HelloWorld.exe
select that and click on the end task button
now you can start debugging and running you'r app.
at least worked for me.

Visual Studio clears output file at end of debugging

I am experiencing a weird behaviour from Visual Studio 2013. I've got a C# program which writes to the standard output, let's say
using System;
using System.Threading;
namespace CsSandbox
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world!");
Thread.Sleep(10000);
}
}
}
In the Debug tab of the project's properties I have redirected the output to a file, like this:
If I open the file within those 10s when my application is still running, the file does contain "Hello world!". However, as soon as the program exits, the file is cleared. This does not happen when I run the program from the command line.
Is there a rationale why Visual Studio does it? Is there a way to bypass this behaviour?
I believe this is due to the way Visual Studio hosts your application, in order to reduce startup time when debugging.
What happens is that your application (Program.exe) will actually be hosted in another process (Program.vshost.exe), which is started with the same command line arguments. When your application ends, this process is immediately restarted. You should be able to see this within Task Manager - look in the details tab so you can see the PID of the processes, and run your app - you'll see one Program.vshost.exe instance which ends when your app finishes, and another one come up immediately. That's then overwriting your output file.
One fix for this is to give your application a command line argument for the file to write to - and open that file within your Main method. You won't get there until you start running the app again.
Another option would be to simply stop using the hosting process - in the Debug part of your project properties, at the bottom you should see a checkbox for "Enable the Visual Studio hosting process". Uncheck this and I think your problem will go away - but it'll take longer to start debugging.
See this blog post for more information about the hosting process.

f# console app doesn't work. c# console app does

I am attempting to deploy a simple f# console app to a coworkers computer but it keeps failing. After double clicking on the app icon, the console window appears but then the Microsoft error reporting window shows up asking if I would like to send the error report, I decline then some text flashes in the console window. It looks like an error message, but the window closes too fast to tell. The weird thing is, if I create a similar C# app, it works. I am targeting .net 4 client framework in release mode.
Here is the code
f# code (doesn't work):
open System
printfn "print test"
Console.ReadLine() |> ignore
c# code (does work):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestCApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing...");
Console.ReadLine();
}
}
}
The F# analog of your C# snippet would be not your F# code, but the following:
System.Console.WriteLine "print test"
System.Console.ReadLine() |> ignore
While the app off 2-liner above will run, similarly to one off your C# snippet, just on raw .NET, use of printfn function in your F# code requires certain F#-specific core components being deployed on the target computer, which is likely not the case. The latter explains the observed behavior.
Look at the references of your F# project and you will see one to FSharp.Core which is normally located here:
C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\2.0\Runtime\v4.0\FSharp.Core.dll
On Windows 7 (64 bit) PC
Also look at the F# site to get the runtime download.
http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/release.aspx
EDIT: here is the direct link to the runtime download (thanks ildjarn):
http://www.microsoft.com/en-us/download/details.aspx?id=13450

Categories

Resources