Active distribution networks (ADN) may operate in different modes according to the generation demand balance and the capacity of the primary grid for imposing a constant frequency. Conventionally, a customized optimization model is used for each operating mode. Unlike that conventional approach, this article proposes a general optimization model capable of operating the system in three different modes: grid-connected, islanded with a surplus of generation, and islanded with a deficit of generation. Real-time operation is required in this framework with guarantees such as global optimum, uniqueness of the solution, and fast algorithm convergence; for this reason, a convex approach is employed for grid modeling. Numerical experiments demonstrate that the proposed optimization-based operation model can handle the three types of operation while ensuring the safety operation with frequency and voltage within expected limits.