Home > front end >  How can I start multiple vscode from c# and keep track of when they terminate?
How can I start multiple vscode from c# and keep track of when they terminate?

Time:10-23

I want to start vscode as an external editor in a program that Im writing in c#. I use the Process class. I can do that but I also like to be notified when a particular instance is closed. Now I run into problems. If I only start one instance of vscode, the process terminates when that window is closed which is what I want. However, if I start a second vscode that process will terminate immediately. This becomes an issue when the user already has an instance open and use my program.

Anyone with an idea on how this can be solved?

CodePudding user response:

Two parts are necessary to answer your question.

The first is the pure C# one: How to start a process and wait for it to end

Everything you need for this is in the Process class.

To start a process: Process.Start

Once started, to wait for exit, you have a few options:

Then there is another part needed to answer the question: How VS Code behaves when started from the command line and its options

By default, VS Code will return immediately after starting (so as not to block a shell if it was started from CLI). So, you'll need to pass a few parameters.

Based on the previous link, those would be:

  • -w or --wait => Wait for the file to be closed before returning.
  • -n or --new-window => Opens a new session of VS Code instead of restoring the previous session. (this is default behaviour, but you might want to specify it anyways in case this ever changes [or the user changes this])
  • Related