How can I run multiple programs sequentially, from one solution? - c#

I have a Visual studio solution containing multiple programs, each of which contains it's own Class and Main Method. I need to set these programs up to run one after the other, as some of them access the same database tables, and I do not want there to be conflicts.
I have tried setting up dependencies and creating a new program with class dependencies, however, this does not seem to have worked, does anyone have an idea?

Right click on your solution > Properties > Common Properties > Startup project
Select "Multiple startup Projects" and change the value of the Action column from "None" to "Start" or "Start without debugging"

Visual Studio is a development tool. It is not a tool to orchestrate how your application should execute. Your application should also function when executed outside Visual Studio.
With that in mind you need to decide how to partition your application. Right now you seem to have several applications (console programs?) that need to execute one after the other. An easy solution is to create a batch file that executes each in turn. You can also create a separate application that execute each "child" application in sequence using the Process.Start method but why bother when you can create a batch file?
You might also consider merging your different applications into a single application that has a Main method that executes each task in sequence.
Without more information about your problem it is hard to give you specific advice.

You mentioned you have tried "Multiple Startup Projects" approach but did you set the order in which multiple projects run when you start the debugger?
To set the order in which multiple projects run when you start the debugger
Open the Solution Property Pages dialog box.
Select the Startup Project set under Common Properties.
In the pane on the right, select Multiple Startup Project.
Select a project, and then click Move Up to run that project earlier when you start the debugger, or click Move Down to run the project later.
Reference: How to: Modify Project Properties and Configuration Settings

You need run each program in separate Visual Studio instances.

Related

Where can i run my code in visual studio?

So, i am trying to write my first C# "Hello World" Program in visual studio, however when i want to see the output of the code (or simply just run it), i can't because i can find the run option, i have tried ctrl + f5, and many other options, however it didn't worked any suggestions? Here's a picture of my visual studio.
Check this guide Tutorial: Create a simple C# console app in Visual Studio
Open Visual Studio 2019.
On the start window, choose Create a new project.
On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.
In the Configure your new project window, type or enter Calculator in the Project name box. Then, choose Create.
And then to run:
Choose the green Start button next to Calculator to build and run your program, or press F5.
This is because you created empty solution. You don't have any output, i.e. process of compilation of your solution does not produce any executable (exe) or library (dll).
In order to run any code is to produce one of the above. In order to do that you have to have project created.
To do so, you have to (in your case) right click solution and choose "Add -> New Project" option. This will show you pop up allowing you to choose which project you want to add, choose just basic type of project, which is "Console app". There you will have class with static method Main which is the entry point for your app.
Code inside Main method will be executed.
You haven't created a project. You've got no executable and no ability to build one. I don't know if you've worked with other languages before or not (perhaps ones which are interpreted and/or have no specific structure to them), but C#/.NET programs require a particular structure in order to build and execute. You can't just create a random C# file on its own and execute it.
Instead, create a new Console Application project (from the File -> New Project menu) and give it a name.
That will auto-generate a Program.cs file, which contains the Main method which is the entry point of your program when it's executed. From there you can write code in there, create other methods in that class if you need to, and you can also add other files containing classes etc. into your project.
(There are, as you'll see, also quite a lot of other project types you can create depending on the kind of application you want - and also some types which are just for code libraries and not full applications.)
How to: Run a C# program in Visual Studio
The project should contain a C# file with a Main method, and its output should be an executable (EXE), you have created an empty solution with no such files.

Start multiple instances of console app in Visual Studio (with different args)?

This question ...and the answer shows how to start another instance of a console app in Visual Studio when you already have one running.
I want to do the same BUT passing different args[]. For example, when debugging I want to start up 2 instances of MyConsoleApp.exe as follows...
"MyConsoleApp.exe Agent1"
"MyConsoleApp.exe Agent2"
Does anyone know how to do this?
(Using Visual Studio 2015 Pro)
As far as I know you can't. What I'd do is open the solution in two VS instances and run it. Or else you can give it a go the following way too ( haven't tried this but ideally it should work)
Create two console projects and add all your files as 'linked' files on both projects ( the same .cs files being used on both projects)
Go to the solution and set one project as 'Start with Debugging' and other as start without debugging. ( or may be both as Start with Debugging- depending on your scenario) here is how you could do this.

Visual Studio: debug multiple projects at the same time?

Is it possible to debug multiple projects at the same time in Visual Studio?
I know you can select multiple startup projects from the solutions properties, but how are the breakpoints handled?
If two projects use the same class (two different instances of it), and I am stopped with a breakpoint in it, will it only block one program or both? How can I know which executable is hitting the breakpoint? I'm a bit confused.
Yes, it is possible. You can set multiple startup projects in your solution (right-click solution, go to Set Startup Projects, choose Multiple startup projects), and specify the action for each project contained in the solution (None, Start, Start without debugging). If you have multiple projects set to Start, the debugger will attach to each one as it launches.
When you hit a breakpoint, you can see which process you're in using the Debug Location toolbar (you may have to show it; some profiles hide this by default). It will show which process you're currently looking at, which thread you're on, and which stack frame you're in:
I believe the default behavior is that when one process breaks, the debugger will break all of them. This way you can check the state of any attached process when you hit a single breakpoint.
No. You can debug an EXE file and step into a debug version of a linked DLL file, if you're careful about making sure the EXE file "sees" the same DLL file as the debugger, but you can't debug two EXE files at the same time. At least as far as I'm aware.

Debugging an application called from another in Visual Studio?

I have a lobby application which invokes a client-application (think: League of Legends). They're two separate applications and the first invokes the second from itself - how can I get Visual Studio to debug this application as well?
You simply need to launch a separate Visual Studio, and then use Debug | Attach to Process to attach to the other process. The trick is using two Visual Studios.
Debug -> Attach to process
Select the executable from the list.
Make sure to select the right code type with the Select.. button.
Have you tried Debug -> Attach to Process?
Ref: http://msdn.microsoft.com/en-gb/library/vstudio/3s68z0b3.aspx
Like Matthew said Debug|Attach to process. If the other application is in a different service you might also want to look into remote debugging
The other answers are correct, but I just wanted to add another approach:
If you add both projects to a single Visual Studio Solution, you could:
Right-click the solution -> Properties -> Common Properties -> Startup Project
There you would select Multiple startup projects and select both the Lobby and the Client applications.
That way you can debug several VS projects without having to run several VS instances.

How to access break points in two application in the same solution

I have two web applications in the same solution. I put break points in both of them and when debug I can't access but the start up one. How to debug both of them?
Just right click on your project solution in visual studio then go to properties and in Common Properties > Startup Project select Multiple startup projects. Then choose the appropriate action.
That way you will be able to debug both
You can have only one active project at time of compilation/running. By the way, if one active project somehow calls something from another, just press F11 and you will jump into another project and symbols for it will be loaded automatically and you can debug it.
EDIT
If you are asking about debugging 2 projects contemporary in parallel, it's not possible with in one VS. If these are the separate projects that communicate with eash others, let's say chat application, you can naturally, by running two instances of VS and debug communication between them.
Hope this helps.
You should try to open two versions of visual studio, run both solutions, but set the start up project differently for each, and run them both. The two instances of visual studio should catch the break points of their respective start up programs. I know this works for windows/console applications. Although I've never tried it with 2 web applications. You may have to configure the applications to use different ports.

Categories