The Immediate Window - visual-studio-2010

We use fluentmigrator and it wants a long for the migration number.
Normally I can just open the immedetiate window and type
System.DateTime.Now.ToString("yyyMMddhhmmss");
But sometimes it will say:
The expression cannot be evaluated while in design mode.
So I start debugging run the command again and get this error:
The expression cannot be evaluated while in run mode.
Edited to add
If I start debugging and hit pause and enter the command I get
Cannot evaluate expression because the current thread is in a sleep, wait, or join
If I start debugging hit a break point then it works but that is too many hoops to jump through and it has worked in the first scenario. It seems to build my project since if there were errors it wouldn't execute.
This is in visual studio 2010 pro.
Bonus points if one can tell me how to do this as a class template or maybe a powershell type thing?
I've tried the answer in Immediate Window, "The expression cannot be evaluated...." and selecting the project doesn't work. I don't get the > in the immediate window and if i put it before the command i get
Command "..." is not valid.
Edited To Add
I created this gist to do what I wanted.
https://gist.github.com/9ad816c2b8e56b57ef79
basically a powershell command to create a c# template with some crap filled in.

Why not just use PowerShell:
PS> [DateTime]::Now.ToString("yyyMMddhhmmss");
20120228043351

I'm not a visual studio user but this is how you can get the value with a PowerShell cmdlet:
Get-Date -Format yyyMMddhhmmss

You need to execute it in the third mode – Debug mode (paused in the debugger).

Related

How do I start a background task from a VisualStudio post-build event?

I'm trying to start an instance of grunt watch whenever a particular project is run from VisualStudio. I have a single page-app written in ember, and compiled with grunt, which connects to a backend written with WebAPI. I'm trying to reduce friction so it's possible to Start Debugging (F5) in VS and have it get everything up and going.
If I add a post-build event to compile the app with grunt, it works fine:
node node_modules\grunt-cli\bin\grunt dev --no-color
grunt watch never terminates, so the VisualStudio build appears to hang until you terminate the node.exe process (which is mostly expected, other than not being able to use Ctrl+Break to stop in VS):
node ../node_modules\grunt-cli\bin\grunt watch --no-color
I've tried starting with the start command, but VisualStudio still waits for it to exit (just saying "Build started..."):
start node ../node_modules\grunt-cli\bin\grunt dev --no-color
I've also tried with start's /i parameter, but that doesn't help. It opens a new window running grunt watch, but the build doesn't continue (and the app doesn't start) until I close the console window.
Presumably this has something to do with the process being a child of the build process? Is there an actual way to start a background task without VisualStudio waiting on it?
Not sure why exactly start doesn't do the trick (works perfectly from the command line), but afaik msbuild spawns a seperate cmd process for it's build events so it will have something to do with that.
A working alternative is to use Powershell to start the process instead. No idea about the builtin powershell syntax, but invoking C#'s Process.Start works just as fine:
powershell -Command "[System.Diagnostics.Process]::Start( '/path/to/node', 'args' )"
That answers your question, however I think it's not what you really want, and you're asking the wrong question.. You say you want to 'start an instance whenever a particular project is run from VisualStudio', but then you go on asking about build events which occur when a project is built. That is different and seems unhandy since every single build will start a new instance. Instead, I think what you actually want/need is to start an instance everytime you start debugging your project. That's also possible as laid out here:
add an empty project to your solution
enter your node command under the projects' Properties->Debugging
right-click solution, select Set Startup Project
select Multiple startup projects
set Action to Start for your main project
set Action to Start without debugging for the empty project
Now hit F5 and VS will start node, plus start debugging your project.

Avoiding "Press any key to continue" when running console application from Visual Studio

When running a console application in Visual Studio via "Start without Debugging" (Ctrl+F5), the console remains open at the end of the run asking to
Press any key to continue . . .
thus requiring to activate the window and hit a key. Sometimes this is not appropriate.
Why this matters:
At the very moment I write json serialisation code, my workflow goes like this:
adapt c# code
run a console app that writes file out.json
view out.json in the browser with a json viewer
do this again and again, no need to debug anything, just trimming serialisation and check output is good.
It is workflows like this, where the "press any ..." behavior is hindering as it requires the steps
activate the console window
press key
.
No answers:
Starting the application outside VS in a separate console is not an answer.
Saying, you dont need this.
I'm pretty sure that you cannot affect or change this behavior.
As you mention, it has nothing to do with your application itself, because it doesn't do it when you double-click on the EXE. You only see this effect when you run the app from within Visual Studio without the debugger attached.
Presumably, when you invoke Ctrl+F5, Visual Studio is running your app in a particular way that causes the console window to remain open. I can think of two ways it might be doing it:
%COMSPEC% /k "C:\Path\To\YourApplication.exe"
or
%COMSPEC% /c ""C:\Path\To\YourApplication.exe" & pause"
With either of these, the pausing behavior you're seeing is baked right into the command used to launch your app and is therefore external to your application. So unless you have access to the Visual Studio sources, you're not going to change it. Calling an exit function from your app won't have any effect because your app has already quit by the time that message appears.
Of course, I can't see why it really matters, aside from an issue of curiosity. This doesn't happen when you start the app with the debugger attached, which is what you'll be doing 99% of the time when you launch the app from the IDE. And since you don't ship Visual Studio along with your app, your users are going to be starting the app outside of VS.
In response to the updates made to your question, the best solution would be to change your app so that it is not a console application. This behavior doesn't affect standard Windows applications; when they get closed, they close for good.
If you do not require any output on the console window, then this is very simple to do: just change the "Application type" in your project's properties. A Windows Forms application will work just fine. If you do not display a window (aka form), one will not be automatically created. This is the difference between regular Windows applications and console applications, which always create a console window, whether you need one or not.
If you do need to display output on the console window, you have a couple of options:
Create and use a simple form with a ListBox or ListView control. Each line that you would normally output to the console, you add as a new item to the list control. This works well if you're not using any "advanced" features of the console.
P/Invoke and call the AllocConsole function to create a console that your Windows application can use. You do not need a form for this.
I found a solution that works if you are using python (I could not test anything else).
You need to go to
Tools -> Options -> Python Tools -> Debugging
Uncheck Wait for input when process exits normally.
I hope you can apply this somehow to your problem.
2020 UPDATE : Microsoft has listened.
It also closes the console in "Start Without Debugging" mode ;)
The setting is a little buried, but works :
Well, at least in Visual Studio 2010, typing
Console.ReadKey(true);
Removes the "Press any key to continue....."
According to the VS2019 documentation:
Automatically close the console when debugging stops: Tells Visual Studio to close the console at the end of a debugging session.
It works, but only if you make sure your project starts with the debugger on. This sounds trivial, but I was trying at first with a solution with two projects, one Console one to copy files to use in my app, the other to run the actual app. I set the Console one to Start without debugging because I don't need debugging on it, but that did not close it after it ran. Only when setting it to Start (with debugging) this option worked.
In vs2017 you have to uncheck the python environment setting under the vs-options:
in german: Auf Eingabe warten, wenn der Prozess normal beendet wird

Avoid starting debugging from the beginning

Is it possible to set a starting point for the debugger so that every debugging session
will start immediately from that point (instead of starting from the beginning of the code)?
Or to express it differently:
Isn't it possible to somehow store everything until the breakpoint so that next time the debugger could just instantly resume to that specific breakpoint (instead of starting from the beginning of the code and pausing at the breakpoint)?. Is there any debugger that can do this?
I am using Microsoft Visual Studio Express 2012.
Thank you.
Use a Debugger in visual studio.
In your code, click on the line number, you will see a dot on the line.
When you run the program, it will 'pause' at the line you specify, you can then walk through your program line by line from there
You can use a breakpoint at a line that you want to inspect.
You have a description how to do it here.
You could attach a debugger to a running process, but i'm afraid that it will be on a random place of execution. You could make a wait for a key or button press in your code and attach to your program before continuing.
No. It would have to run the code up to the point you want to get all the variables etc in the right state. If you just set a breakpoint where you're interested from and hit F5 it should get there quickly enough.
If it doesn't get there quickly enough, jot down the variables used and make some unit tests round the troublesome functions instead. That will skip the 10 minutes.

Redirecting 'nunit-console' output to the Visual Studio output window

I am testing an F# project using NUnit. On the debug tab of project configuration I have set the debugger to use an external program which is nunit-console here and the working directory to the debug folder of my project. I prefer using nunit-console with the debugger since the GUI version doesn't hit the test file's breakpoints.
When I debug the test the console window appears and disappears and there is no chance to read the output. I have tried Console.Readline(), but it doesn't work because when I directly run the test from a terminal using nunit-console, it fails due to this command.
Is there a way to redirect the nunit-console output to the Visuals Studio's output window?
UPDATE: I could use Thread.Sleep() to delay the nunit-console.exe when I run the test from the console. But from Visual Studio it doesn't happen so I am pretty sure that nunit-console.exe fails to read the test file when the command is issued by Visual Studio. Still, it would be very nice to be able to read the console output, thus the redirection is still desirable.
Either use Tools->Options...->Debugging->General:"Redirect all Output Window text to Immediate Window" and then make sure that the "Immediate Window" is visible with Debug->Windows->Immediate.
Or use NUnit's "/wait" command line option.
Unless I am missing something, you should be able to hit all your breakpoints with the GUI as well, set the startup project to nunit.exe and pass the name of your test DLL as a command line parameter. You will hit the breakpoints in Visual Studio, and your print statments will be in the GUI's output tab.

Immediate Window for Eclipse

Does Eclipse have an analog to Visual Studio's "Immediate Window", a window where I can evaluate statements while in the debugger?
Yes. The view name is "Display".
Window->Show View->Other
It is under the Debug folder.
Once in there you evaluate statements while in the debugger.
Eclipse has a really cool concept call Scrapbook Pages where you can evaluate statements even when you're not debugging. However, if you want to eval code using values from the current program, go to Window->Show View->Expressions. There you can put in any expression you want and track it as your program executes.
Inspect ctrl-shift-i or Display ctrl-shift-d?

Resources