We propose a method to perform a safe unintentional islanding maneuver of microgrids. The method is derived in the context of a framework for the real-time control of microgrids, called Commelec, recently proposed by the authors. The framework uses a hierarchy of software agents that communicate with each other using a common, device independent protocol in order to define explicit power setpoints without the need of droop controllers. We show that the features of the framework allow to design a generic control method for treating unintentional islanding with the following properties. First, the method is able to choose the best candidate slack resource, based on the information obtained from the agents. Second, as the agent responsible for the grid has a global view of the network's status and its resources, it is possible to optimize the performance of the network during and after the islanding transition. Third, after the islanding maneuver, it allows for the online switching of the slack resource to that with the best capabilities to face the network's needs. Finally, the method is suitable for inertia-less systems as the control is performed using explicit power setpoints and it does not rely on the frequency signal. We illustrate the benefits of the proposed method via simulation on the LV microgrid benchmark defined by the CIGRE Task Force C6.04.02, by comparing its performance to that of the standard droop-based method called load drop anticipator.