Coder Social home page Coder Social logo

solutionloadsample's Introduction

Solution Load sample

Build status

Applies to Visual Studio 2015 and newer

This example shows how to listen to solution events when the package might not be initialized until after the solution has loaded.

Clone the repo to test out the sample in Visual Studio 2017 yourself.

What is the problem?

In Visual Studio 2017 Update 8, packages will no longer be auto-loaded immediately when the following is true:

  • Package inherits from AsyncPackage
  • Uses ProvideAutoload attribute
  • Supports background load
  • VS is starting up or solution is being loaded

Instead, the package will be initialized after the startup or solution load depending on the ProvideAutoload context. This is done for performance reasons and is generally speaking a net benefit to users.

The consequence is that the solution might already have been loaded when your package initializes and no solution load events will be fired until the user opens another solution.

See full Package class in the source

The new pattern

...happens to be what was always considered a best practice. Here are the steps:

  1. Specify the package to autoload when a solution is opened
  2. Check if a solution is open when package initializes and act (new)
  3. Add event handlers for solution open events

Step #2 has always been considered a best practice, but now it is a mandatory step.

It used to be ok to autoload when a solution opened and then hook up the event handler, similar to this:

[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOpening_string, PackageAutoLoadFlags.BackgroundLoad)]
public sealed class VSPackage : AsyncPackage
{
    protected override Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
    {
        SolutionEvents.OnAfterOpenSolution += HandleOpenSolution;
        return base.InitializeAsync(cancellationToken, progress);
    }

    private void HandleOpenSolution(object sender = null, EventArgs e = null)
    {
        ...
    }
}

The issue in the above sample is that when the SolutionEvents.OnAfterOpenSolution event handler is registered, a solution might already be open. So we need to make sure to check that first, like so:

[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionOpening_string, PackageAutoLoadFlags.BackgroundLoad)]
public sealed class VSPackage : AsyncPackage
{
    protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
    {
        bool isSolutionLoaded = await IsSolutionLoadedAsync();

        if (isSolutionLoaded)
        {
            HandleOpenSolution();
        }

        SolutionEvents.OnAfterOpenSolution += HandleOpenSolution;
    }

    private async Task<bool> IsSolutionLoadedAsync()
    {
        await JoinableTaskFactory.SwitchToMainThreadAsync();
        var solService = await GetServiceAsync(typeof(SVsSolution)) as IVsSolution;

        ErrorHandler.ThrowOnFailure(solService.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out object value));

        return value is bool isSolOpen && isSolOpen;
    }

    private void HandleOpenSolution(object sender = null, EventArgs e = null)
    {
        ...
    }
}

See full Package class in the source

This simple check for IsSolutionLoadedAsync() is all we have to do and we can now handle the solution open as usual.

Further reading

License

Apache 2.0

solutionloadsample's People

Contributors

aarnott avatar japj avatar madskristensen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

aarnott

solutionloadsample's Issues

Acknowledgement

Hi Mads,

Thank You for this sample. It helped me to migrate my project to AsyncPackage.

Regards,
Josef

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.