2

In the Package constructor I added the event handler for OnStartupComplete event. But when I run the code, the the event handler is not called. What am I doing wrong?

dattebayo
  • 2,012
  • 4
  • 30
  • 40
  • Packages are deferred loaded by default. Are you telling Visual Studio to load your package on startup? see the answer to this question: http://stackoverflow.com/questions/7815461/vsix-execute-code-on-vs-startup –  Jan 09 '12 at 15:56
  • Will, I used the PackageAutoload attribute, but this attribute works only if VS is launched by a project or solution file. If I open only the VS to open the StartPage, the package is not loaded!! – dattebayo Jan 12 '12 at 01:58
  • I don't believe that is true. There are other attributes you can put on your package which tells VS that your package should be loaded only when a solution is present. You sure you're not using one of those? –  Jan 12 '12 at 14:12
  • The only attributes I have on my package are, PackageRegistration, DefaultRegistryRoot, ProvideLoadKey, ProvideMenuResource, ProvideAutoLoad, ProvideToolboxItems and Guid. – dattebayo Jan 17 '12 at 16:23
  • Use `[ProvideAutoLoad(UIContextGuids80.SolutionExists)]`, `[ProvideAutoLoad(UIContextGuids80.NoSolution)]`, `[ProvideAutoLoad(UIContextGuids80.EmptySolution)]` as attributes and the package will be loaded no matter what. – Jerric Lyns John Oct 08 '14 at 13:52

3 Answers3

4

There's a bug in VS that recycles the DTEEvents object (with your event handlers) unless you keep an explicit reference to it. You need something like this:

[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string)]
class MyPackage : Package
{
    DTEEvents _EventsObj;

    protected override void Initialize()
    {
        var dte = (EnvDTE.DTE)GetService(typeof(EnvDTE.DTE));
        _EventsObj = dte.Events.DTEEvents;
        _EventsObj.OnStartupComplete += OnStartupComplete;
    }

    void OnStartupComplete()
    {
    }
}
Ivan Shcherbakov
  • 2,063
  • 14
  • 23
0

Try moving your code from package constructor to the Initialize() method of the package. It should help, but if it doesn't, test some other UICONTEXT_??? values for your AutoLoad attribute, maybe UICONTEXT_NoSolution ?

cre8or
  • 387
  • 3
  • 10
0

See my answer here: http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/eb1e8fd1-32ad-498c-98e9-25ee3da71004

I believe that it is because you could be boxing and unboxing your DTE object before doing the event subscription. This is a huge nuisance, and quite surprising that the DTE object cant be passed around easily through service location for the purpose of event subscriptions; but this seems to be the culprit.

I had tried to keep a reference to the DTE object but it made no difference as I was doing that anyway. Some events will work, and some won't; but this is consistent.

Dessus
  • 2,147
  • 1
  • 14
  • 24