5

Is there the possibility to run some code when the Code is stopped when running it from Visual Studio?

I am using the CefGlue library to build a WinForms application and realized that there are issues when pressing the stop button ranging from Exception to two windows with no content opening. A separate process continues to run in the background. In order to stop Cef nicely I need to exectue CefRuntime.Shutdown(); Maybe this is because it does not run the application in a Visual Studio hosting process, because CefGlue has problem with this (see this). This does not affect production but is nasty while developing and testing, but nevertheless I would like to execute some code to fix the problem.

I would guess this is not possible but if it was it would be interesting to know.

So I am looking for a way to execute some code when Visual Studio is stopping the application when pressing the stop button while in development.

Note: I am using Visual Studio 2013 and 2015.

Edit The issue is not reproducibly with very few lines of code. Nevertheless I have tried to create a simplified example here

Community
  • 1
  • 1
Sjoerd222888
  • 3,228
  • 3
  • 31
  • 64
  • What type of application are you building? – MikeS159 Jan 27 '16 at 10:17
  • I am building a WinForms application. – Sjoerd222888 Jan 27 '16 at 10:21
  • 1
    I was going to say you should have a form close event which clears up any threads before the program exits properly, but this is not called when you stop it in Visual Studio. I've not come across VS being unable to close parts of the program before. Can you post the code where the process that continues to run is set-up? – MikeS159 Jan 27 '16 at 10:32
  • I want to run`CefRuntime.Shutdown();` at the end of the application and I would like to run this when Visual Studio closes. Cef runs in a separate process and this process is not stopped when Visual Studio finishes. – Sjoerd222888 Jan 28 '16 at 09:54
  • If you run your program on its own, then kill the program in task manager, does the Cef process keep running as well? – MikeS159 Jan 28 '16 at 09:59
  • Kill it in the task manager does stop all tasks pressing stop in VS does not. – Sjoerd222888 Jan 28 '16 at 10:04
  • That's weird, I assumed. I was going to suggest since you probably don't want the Cef process running when the form is closed, you could add something to it where it check the form process is still alive, and closes its self if the form has been shut without using the form close button. However, that might be overkill if the problem is only in debugging and not in general usage. – MikeS159 Jan 28 '16 at 10:07
  • @Mike159 I have create a simplified example. It does however crash. Just click on continue and you will have those two windows appearing. – Sjoerd222888 Jan 28 '16 at 10:42

1 Answers1

1

What you're basicly looking for is a solution using the Visual Studio SDK.

You can build your own add-ins by implementing the IDTExtensibility interface.

In the OnConnection function you can subscribe to different events. Using (DTE2)application you can access a lot of things from VS.

You'll have to subscribe to some of the events that can be obtained from the Events property.

You'll have to find out yourself what events work best for your solution. But the DebuggerEvents would seem like a good place to start.

This does require some research before you can use it. There are likely to be easier solutions.

As a simple example for the OnConnection:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    var applicationObject = (DTE2)application;

    var events = _applicationObject.Events;
    var buildEvents = (BuildEvents)events.BuildEvents;
    buildEvents.OnBuildBegin += new _dispBuildEvents_OnBuildBeginEventHandler(OnBuildBegin);
}

This triggers when a build is started. The available documentation isn't great, so it will need some trial-and-error before you find what you need.

Chrono
  • 1,433
  • 1
  • 16
  • 33