4

Is it possible to attach the Visual Studio to a process right at the moment when that process starts? Ideally I would like for VS to listen to processes starting until one meets a certain condition, at which point it would immediately attach itself to that process.

In other stack overflow questions I've seen the suggestion to start the process from my own visual studio instance, but in my situation this is not possible. I need to attach VS to a debugger thread in another VS instance with an SSIS project. The debugger thread must be started by that VS instance. I can not attach my VS instance to the process quickly enough by hand to debug certain processes that happen immediately at the start of the thread.

NRade
  • 307
  • 2
  • 12
  • 1
    [Launching the Debugger Automatically](https://msdn.microsoft.com/en-us/library/a329t4ed(VS.71).aspx) is quite old but still valid, I believe. – Damien_The_Unbeliever Aug 16 '16 at 08:46
  • I may mark this as the answer if you post it as an answer rather than a comment. In my example situation it causes the SSIS project to crash ("Cannot communicate with the debug host process. Failed to obtain child process active object."), but it answers the main question of the thread perfectly. – NRade Aug 17 '16 at 08:23
  • Does this answer your question? [Visual Studio: auto attach to a process when the process is spawned](https://stackoverflow.com/questions/1951124/visual-studio-auto-attach-to-a-process-when-the-process-is-spawned) – StayOnTarget Oct 06 '22 at 12:17

2 Answers2

2
System.Diagnostics.Debugger.Break()
citykid
  • 9,916
  • 10
  • 55
  • 91
  • I would give this 10 upvotes if I could, because it looks extremely useful and may be something I put in a majority of the code I write from now on. However, in this particular situation it causes the SSIS debug host to crash without clear error reporting. There may be some complications stemming from the fact that the process I'm trying to attach to is itself a debug host. – NRade Aug 16 '16 at 09:49
  • 1
    glad the info was useful. too bad it does not help in your very special situation. I have no instant idea what could help here. Anyway, the System.Diagnostics.Debugger object has some useful members, Break() being the most frequently used. – citykid Aug 16 '16 at 10:07
  • 1
    Beware this may not work if your application runs with elevated permissions. Use if (Debugger.IsAttached == false) Debugger.Launch(); instead, and you will see a warning about running the Just In Time Debugger elevated. Accept that and it works as required. – Andrew Roberts Oct 27 '22 at 13:05
1

It's not solution for all cases but simple way is to add this code at begining. I didnt have VS on remote station but I was able to attach remotely and bellow lines saved my day For Windows application

Windows.Forms.MessageBox.Show("wait attach process and click ok");

For ConsoleApp

Console.WriteLine("wait attach to process and press any key");
Console.Readkey();
mr R
  • 997
  • 2
  • 12
  • 25