How do I attach Visual Studio to a process that is

2019-01-21 05:12发布

I have .NET program that can't be run from Visual Studio for some reasons (Excel file created from an Excel 2010 template project) for which I need to debug startup events.

If I want to debug events that comes after program initialisation there is no problem. I run the program from the explorer, attach the process to Visual Studio and add some breakpoints in the code. But here, I need to put breakpoints on the startup events. I need to be able to attach processes to Visual Studio, not depending on a PID, but on a specific process name or whatever other solution that would work.

Of course adding a Thread.Sleep(1000) in my startup events to give me some time to attach the process in Visual Studio is out of the question!

7条回答
Animai°情兽
2楼-- · 2019-01-21 05:48

If there's no process then Visual Studio can't attach to it.

However, you can set the startup program of your project to be something other than the output of your project.

查看更多
爷的心禁止访问
3楼-- · 2019-01-21 05:53

Actually you can; you don't attach to it, you start it. On the properties of your project, on the Debugging tab, specify the path of the program you want to attach to in the "Command" textbox.

You can also enter any command-line arguments for the program in the "Command Arguments" box:

enter image description here

Ensure that "Attach" is set to "No".

查看更多
SAY GOODBYE
4楼-- · 2019-01-21 06:00

Follow these steps if you have Visual Studio 2017:

  1. File > Open > Project/Solution
  2. Choose your .exe
  3. Debug > Start Debugging

Much easier than other suggestions: you don't have to mess with project properties, and no extensions are needed.

查看更多
smile是对你的礼貌
5楼-- · 2019-01-21 06:00

You can show a MessageBox, this would block the application, then you attach or reattach the debugger to the process and click ok to continue:

MessageBox.Show("Attach process to debugger and click ok!");

you can add it to the Form constructor (if you use winforms), so this would be executed before anything else, except for the initialization of components:

public MainForm()
{
    InitializeComponent();
    MessageBox.Show("Attach process to debugger and click ok!");
}

When you finish your debugging, comment out that line.

查看更多
男人必须洒脱
6楼-- · 2019-01-21 06:04

One little solution that might suit many people.

  • in the first line of code that the exe will run, add this command

    System.Threading.Thread.Sleep(20000)

That will make the exe sleep for 20 seconds before it starts processing anything. Then you have 20 seconds to attach to the process, which can be done quickly with ctrl+alt+p, then find the process, then enter to attach.

Not much of an answer but worked a treat for me :--)

查看更多
再贱就再见
7楼-- · 2019-01-21 06:08

I was debugging a C++ plugin in an externally spawned process that crashed by throwing an exception at startup and this worked perfectly for me:

Add the free Reattach Extension for Visual Studio. Ask it to reattach to the process name before it is launched. It will pop a modal dialog saying it is waiting for the process name to launch.

Now launch the process and the Visual Studio debugger will attach immediately, catching exceptions and hitting breakpoints.

查看更多
登录 后发表回答