Starting new process - c#

Under my solution I have 2 projects Project A console application , Project B Winforms Window.
How I can From Project B to start new process with console application from Project A ?
I tried to making it via
Process note = new Process();
note.StartInfo.FileName = "note.exe";
note.StartInfo.Arguments = "123";
note.Start();
But in that way i cant debug both processes from one vs instance . Any idea ?
Thanks

If the only reason that you want to start the two projects is that you want to be able to debug both of them you can also configure your solution to have multiple startup projects:
Solution -> Properties -> Set Startup Projects...
If you are going to start the other project as a separate project anyway, you could use
Debug -> Attach to Process
to attach to the newly started process. Then you will also be able to debug both processes in Visual Studio. Attaching the debugger can also be achieved programmatically. In project A you can add the following code to the Main method:
#if DEBUG
System.Diagnostics.Debugger.Launch();
#endif
In the dialog that will then pop up you can select the already running instance of Visual Studio.

Another option is to start the Startup Project via F5 (or Debug > Start Debugging) and the second one by right-clicking the project and going Debug > Start New Instance.

You can start debugger in the application launched by the following statement
System.Diagnostics.Debugger.Break()
then you can choose the visual studio instance and start debugging.

Related

Hololens: no debug output in Visual Studio 2017

My task is very simple, I put in the Main method two lines:
Debug.Log("App START Debug.Log");
Debug.unityLogger.Log("App", "START Debug.unityLogger.Log");
and want to see these logs in the output if the app is running. If I set the run option as Releasethe app starts, but I don't see these logs. If I set the run option as Debug and try to start, I get an error popup:
Unable to activate Windows Store app ´Template3D_pzq3xp76mxafg!App´
and the app doesn't start at all.
EDIT: according to comment of #Perazim, I have also tried:
System.Diagnostics.Debug.WriteLine("App START Debug.Log");
No effect in Release and Debug mode.
EDIT 2:
System.Diagnostics.Debug.WriteLine("App START Debug.Log");
works, if I put it in other methods, but doesn't work in the Main() method.
Debugger.Log for the output to debugger. Debug.WriteLine prints to listeners. If there is no listeners, it will not work.
My application stopped writing output to the output window with the System.Diagnostics.Debug.Writeline in debug mode. This all happened after the latest Microsoft Updates.
I created a new application just to see if it was my current application, and the new application would not write to the output window either.
Then I decided to right click on the Output Pane in Visual Studio and what do I see, Program Output unchecked.
I checked this and my Debug.Writeline works again.
In unity you can use/see debug.log(); in Visual Studio you need to use Console.WriteLine();

Running Multi Startup Application does not stop the application on Release but stops them on Debug

I have a question on the difference off the Debug and Release build run for Visual Studio 2010. I have a solution that has two project A and B. A depends on B and calls B's method thru interprocess communication IPC.
Here are the scenarios:
CASE A:
Run B only
- Stops the window when you click the STOP button on both on Debug and Release
CASE B:
Multi Startup Run Both A and B (Both action are set to "Start"):
- Irregardless of the order of run,
On Release run, A is STOP but B is not stopped
On Debug run, B and A are both stopped
I am wondering what is the difference when running multi startup on Visual Studio on Debug and Release versions. The odd thing that I can think of is B registers an IPCServiceChannel on Startup. But why the problem is B can stop properly when I just run it by itself.
IpcServerChannel serverChannel = new IpcServerChannel("ProcessName");
ChannelServices.RegisterChannel(serverChannel, true);
Any suggestions how to fix this?
Set up for Multi Startup for more than project requires setting up Action for every project
Actions such as : None, Start & Start without debugging
Also one more important thing is you need set the order in which the project should start
For example if you want your Server to start first but if it is last in the order than it will start last in order. You will have to move the Server project to execute first and then the rest in order
Conclusion : In a multi start up project you must set the action and the order for every project

Selecting which project under a solution to debug or run in Visual Studio 2010

This one should be easy. I just can't figure out what to search for...
For this one solution I created a unit test project, and I've been adding unit tests frantically. When I went back to try to run the original project after making all the unit tests pass I realized that I couldn't figure out how to debug the original project.
In other words, every time I try to "debug" (e.g., by pressing F5), Visual Studio will run the unit tests. So the question is how do I run various projects in a single solution? How do I select which one will run when I want it to?
You can right click on the project and choose to set as startup project
You can right-click a project and set it as the Startup Project. It then is debugged when you press F5, or run when you press Ctrl-F5.
You can right-click a project and select Debug / Start new instance to debug a project that is not the startup project.
On a side note, you can run / debug the unit tests without setting the unit test project as the startup one. There are icons (and keyboard shortcuts) in the toolbar to run or debug unit tests at various scopes (if not visible right-click your toolbar and select Test Tools).
1) In the Solution-Explorer, right-click on the project that is the project for the executable assembly, and select "Set as Startup Project". This will make the project show in bold, and will be run automatically when you press F5.
2) Alternatively, in the Solution-Explorer, right-click on the project that is the project for the executable assembly, and select "Debug -> Start New Instance". This will start the debug session immediately.
This was my quickly Startup Project change way for me:
Tools > Customize > Commands Tab.
Choose Toolbar selection
Select Standard toolbar.
Choose a place by mouse for new icon on menu preview and click Add Command... button
Go to Project item on Categories list on opened Add Command window.
Select Set as Startup Project item on Commands list
Now, you can Set current project as Startup Project just one click
But today I want more quickly way, then I finally found a way to set a shortcut for Set as Startup Project menu.
Just set a shortcut for "ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance" item at Tools > Options > Enviroment > Keyboard
I set Ctrl + Alt + Shift + F5
I wanted to debug a single console app project, and it is as simple as that:
Select the project in the Visual Studio run menu:
You can even add debug parameters in the project properties:
That's it. You can right-click the project and press "Start new instance",
or press F5, or click the "play" button visible in the first image.
Now in Visual Studio 2020, when you right click on the solution and click on Properties you have the choice of running a single or multiple project or multiple :
If you choose single you can choose here :
And with multiple startup projects, you can run multiple at the same time !

Debugging processes spawned by another

I have written a program that spawns a handful of processes.
By default, Visual Studio does not debug the new processes -- only the original process that created the new ones.
Is there a way to automatically, in code, connect Visual Studio up to the processes when they are created?
From: debugging a process spawned with CreateProcess in Visual Studio
You can temporarily put in a call to DebugBreak() somewhere in the
startup code of your child process. This will cause Windows to prompt
you if you want to debug that process.
EDIT:
Since both projects are in the same solution, configure VS for multi-project debugging:
(VS2010)
Context-click the solution node in Solution Explorer
Choose Set Startup Projects...
On the dialog select Multiple startup projects radio button
In the Project grid change the Action for all the projects you wish to debug to Start
This still provides a prompt, but does attach the debugger:
if (!Debugger.IsAttached && DebuggerFlagSet()) Debugger.Launch();
and then in the parent process
if(Debugger.IsAttached)
SetDebuggerFlag()
You'll need a mechanism for the debugger flag such as a file on disk/reg key/mutex etc.
The debugger being launched won't be the same as the initial instance.
To do this, you'll have to write a VS Add-in or a VS Package, because you'll want to sit in the background and wait for child processes to load.
Here's a general recipe of what you'll want to do:
Get the ID of the debuggee process (ie, uint processID = DTE.Debugger.CurrentProcess.ProcessID)
Add reference to System.Management and use a ManagementEventWatcher to listen for new processes creation as described in this thread. Your query should be "SELECT ProcessID FROM Win32_ProcessStartTrace WHERE ParentProcessID = " + processID
When a new child process loads, find it by its processID in DTE.Debugger.LocalProcesses, then call .Attach() on it.

How to start a child process in the same Visual Studio debugging session as the parent, programmatically?

When running a process under the debugger, I would like to start a child process in the same debugger.
Currently, I use
Process.Start("sample.exe");
I want it to be something like this:
if (Debugger.IsAttached)
// start "sample.exe" in the same debugging session
else
Process.Start("sample.exe");
I could pass a flag to the child process that instructs it to call Debugger.Launch(), but that won't catch start up errors, and it results in a debugging session where some features are not enabled (such as edit and continue, etc). It's preferable to have the debugger launch the process directly.
You should attach debugger to process you are starting. This could be done:
Manually from Visual Studio after starting "sample.exe" select it menu Debug > Attach to process..
Programmatically attach debugger inside "sample.exe"
Attaching to a Process using VS.NET Automation Model
UPDATE: You can setup windows environment to attach debugger every time "sample.exe" starts: Launch the Debugger Automatically (you will need to call Debugger.Break anyway)
Some external tool maybe
Here is code for "sample.exe" to attach debugger:
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
You should pass some parameter to "sample.exe" to verify if you need to attach debugger.
Process.Start("sample.exe", "Debug=true");
you can change the properties of your solution to start multiple apps.
an explanation is here Run Multiple projects
The MSDN article is here MSDN Article on running multiple projects
Assuming that sample.exe would be the host for code you want to debug, you can use project properties -> debug settings - choose action as an external program and provide path to sample.exe. The VS will start this executable and attach the debugger to it.
Visual Studio Debugger Team created an extension that does that automagically:
https://marketplace.visualstudio.com/items?itemName=vsdbgplat.MicrosoftChildProcessDebuggingPowerTool

Categories

Resources