This paper addresses the operation problem of microgrids under market environments. Considering the flexibility of DG sources in operation as well as the availability of market prices, microgrid operators can improve the profit in electricity markets by properly adjusting DG units in real-time corresponding to the price signal which is updated few-hour ahead. Starting with the basic formulation of unit commitments, the paper derives the optimality condition of DG units, i.e., generation levels for maximizing the profit assuming all units are being On (committed). If the capacity constraint results in an excess of the marginal cost to the locational market price, the unit will be considered to be Off. Taken into account the cost associated with switching On/Off the unit, an economic comparison will determine the optimal decision of whether to turn the unit On/Off or remain its current state. The proposed operation scheme is examined in a case study to illustrate its effectiveness with different scenarios of the market price.