public static GraphicsDeviceService AddRef(IntPtr windowHandle, int width, int height)
 {
     if (Interlocked.Increment(ref refCount) == 1)
     {
         instance = new GraphicsDeviceService(windowHandle, width, height);
     }
     return instance;
 }
        protected override void Dispose(bool disposing)
        {
            if (deviceService != null)
            {
                deviceService.Release(disposing);
                deviceService = null;
            }
            if (contentManager != null)
            {
                contentManager.Unload();
                contentManager.Dispose();
                contentManager = null;
            }
            if (spriteBatch != null)
            {
                spriteBatch.Dispose();
                spriteBatch = null;
            }

            base.Dispose(disposing);
        }
        protected override void OnCreateControl()
        {
            // Don't initialize the graphics device if we are running in the designer.
            if (!DesignMode)
            {
                deviceService = GraphicsDeviceService.AddRef(Handle,
                                                             ClientSize.Width,
                                                             ClientSize.Height);

                // Register the service, so components like ContentManager can find it.
                services.AddService <IGraphicsDeviceService>(deviceService);

                contentManager = new CustomContentManager(services, "Content");
                spriteBatch    = new SpriteBatch(GraphicsDevice);

                // Give derived classes a chance to initialize themselves.
                Initialize();
            }

            base.OnCreateControl();
        }
        protected override void Dispose(bool disposing)
        {
            if (deviceService != null)
            {
                deviceService.Release(disposing);
                deviceService = null;
            }
            if (contentManager != null)
            {
                contentManager.Unload();
                contentManager.Dispose();
                contentManager = null;
            }
            if (spriteBatch != null)
            {
                spriteBatch.Dispose();
                spriteBatch = null;
            }

            base.Dispose(disposing);
        }
        protected override void OnCreateControl()
        {
            // Don't initialize the graphics device if we are running in the designer.
            if (!DesignMode)
            {
                deviceService = GraphicsDeviceService.AddRef(Handle,
                                                                     ClientSize.Width,
                                                                     ClientSize.Height);

                // Register the service, so components like ContentManager can find it.
                services.AddService<IGraphicsDeviceService>(deviceService);

                contentManager = new CustomContentManager(services, "Content");
                spriteBatch = new SpriteBatch(GraphicsDevice);

                // Give derived classes a chance to initialize themselves.
                Initialize();
            }

            base.OnCreateControl();
        }