'How to start the same project many times in Visual Studio

How can I automatically start the same project many times in Visual Studio 2015?

I know you can select different projects by doing.

Go to Solution properties -> Common properties -> Startup Project and select Multiple startup projects

I also know that you can right-click on it in Solution Explorer and choose Debug-> Start new Instance, to launch a new copy of it.

But I want to start the same project 5 times and I don't want to repeat the steps above 5 times each time I debug.

Thanks



Solution 1:[1]

You could exploit the fact that you can add an executable as a "project" in your solution. Here's a really dirty solution to your problem:

  1. In your post-build step of the Project, create 4 extra copies of the .exe
  2. Build your project once.
  3. Add the multiple .exes as "Projects" (right-click folder > Add > Existing Project, then select your exes, one by one)
  4. Now you can set up your "Multiple StartUp Projects" to launch all 5 "projects", and debug them at the same time.

Solution 2:[2]

How can I automatically start the same project many times ...But I want to start the same project 5 times and I don't want to repeat the steps above 5 times each time I debug

You can't.

You can however press Ctrl+F5 five times. Which to me is the default keyboard short-cut for Debug.Start without debugging.

It's a little bit easier than the mouse but still a manual task.

Solution 3:[3]

I finally found a relatively "nice" solution, but it requires the use of containers. I've added Docker support to the binary, and orchestration support for docker-compose. Now, I can do this in my docker-compose.yml:

version: '3.4'

services:   
  myapp.myappd:
    image: mcr.microsoft.com/dotnet/runtime:5.0-nanoserver-1903
    depends_on:
        - myapp.myappd.client
        - myapp.myappd.server   
  myapp.myappd.server:
    image: ${DOCKER_REGISTRY-}myappmyappd
    environment:
        - CONFIGFILE=config/config_server.txt
    build:
      context: .
      dockerfile: Myapp.Myappd\Dockerfile   
  myapp.myappd.client:
    image: ${DOCKER_REGISTRY-}myappmyappd
    environment:
        - CONFIGFILE=config/config_client.txt
    build:
      context: .
      dockerfile: Myapp.Myappd\Dockerfile

Every service requires either "build" or "image". As I only want to run my app 2 times (once as server, once as client), I specify some random image. The trick is the "depends_on", which causes the actual client/server to start. In my app, I use Environment.GetEnvironmentVariable("CONFIGFILE") to differentiate between the client and the server.

Solution 4:[4]

I just updated my program.cs of the winforms project to spawn forms on seperate threads like this:

internal static class Program
{
    /// <summary>
    ///  The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // To customize application configuration such as set high DPI settings or default font,
        // see https://aka.ms/applicationconfiguration.
        ApplicationConfiguration.Initialize();
        for(var i = 0; i < 3; i++){
            var localI = i;
            var thread = new Thread(() => {
                var form = new Form1();
                form.Left = form.Width * localI;
                Application.Run(form);
            });
            thread.Start();
        }
        var resetEvent = new ManualResetEvent(false);
        resetEvent.WaitOne(); // Just wait forever
    }
}

You can just create a debug-project containing just this program.cs used only for launching x instances for debugging purpose in VS. Then you wont touch the original program.cs which you need in production to open just a single form.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Rainmaker
Solution 4 Stephan Møller