Problem with ToggleFullscreen MonoGame 3.6

Hello everywone,

I have problem with exiting game window but it freezes like it cannot exit game.

MonoGame.cs from Engine Directory
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace HLMono
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class MonoGame : Game
    {
        public GraphicsDeviceManager graphics
        {
            get; set;
        }

        public SpriteBatch spriteBatch
        {
            get; set;
        }
        
        public MonoGame()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 640;
            graphics.PreferredBackBufferWidth = 820;

            Content.RootDirectory = "Content";

            graphics.ApplyChanges();
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>



        protected override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

Client.cs from Engine directory
using Microsoft.Xna.Framework;
using System;

namespace HLMono.Engine
{
    public class Client
    {
        public MonoGame jGame
        {
            get; set;
        }

        private GraphicsDeviceManager jGraphics
        {
            get; set;
        }

        private string jWidth = "";
        private string jHeight = "";

        public int Host(string[] args)
        {
            jGame = new MonoGame();
            jGame.graphics.IsFullScreen = true;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "-width" || args[i] == "-w")
                {
                    jWidth = args[i + 1];
                    jGame.graphics.PreferredBackBufferWidth = int.Parse(jWidth);
                }

                if (args[i] == "-height" || args[i] == "-h")
                {
                    jHeight = args[i + 1];
                    jGame.graphics.PreferredBackBufferHeight = int.Parse(jHeight);
                }

                /**
                 *  -windowed
                 */
                if (args[i].Equals("-windowed"))
                {
                    jGame.graphics.IsFullScreen = false;
                    jGame.graphics.ApplyChanges();
                }
            }

            jGame.Exiting += OnExiting;

            jGame.Run();

            return args.Length;
        }

        private void OnExiting(object sender, EventArgs args)
        {
            jGame.graphics.ToggleFullScreen();
            jGame.Exit();
        }
    }
}

And Program.cs
using HLMono.Engine;
using System;

namespace HLMono
{
    /// <summary>
    /// The main class.
    /// </summary>
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main(string[] args)
        {
            Client _cl = new Client();
            _cl.Host(args);

            return 0;
        }
    }
}

Why I need it. because I want create real game with arguments like any games have same arguments

example
game.exe -w 800 -h 600 -windowed = It means windowed version with game or
game.exe -w 1024 -h 768 = It means fullscreen mode with filled monitor

I have problem with exiting of game-window. Why does it force to exit. If I click close Button. It doesn’t happen…

Thanks for help me!

Don’t call ApplyChanges in your Host function. When exiting, don’t toggle full screen, only exit full screen when your game is running in full screen.

If you still run into issues after those changes, you might want to try a develop build of MonoGame. Some fixes have been merged in since 3.6.

@Jjagg thanks explanation!

But it still freezed if I click border of game window . But it looks like it will be cold.

Check Youtube !

You know that Game should be external like any game. If you use SDL2 C# and it shows fullscreen or windowed with passing argument like any games have passing arguments.

Yes I want try to make own game

// EDIT:
Thanks for explanation about build development of monogame from Github. I tried to download and Protobuild.exe --generate Windows than it works for me fine now. Wow! fullscreen exit works fine. Woah good job!

But I wish I have support for OpenGL.net if it works more than OpenTK. Because OpenTK has problem of fullscreen if it looks issus

Thanks!

I think the issue might have been the jGame.Exit() call in OnExiting. Anyway, I’m glad you don’t have the issue with a develop build :slight_smile:

I’m not sure what you mean. MonoGame can use OpenGL if you pick a Cross Platform Desktop Project instead of a Windows DirectX Project.

Thanks for saying about current development build of MonoGame works 100%.

It means “OpenGL.Net”, not “OpenGL”

It works fine. Thanks I will replay later If I have problem :slight_smile:

// EDIT:

For Mac OS X Monogame crashes if I use exit. I already use development build from Github than it happens because it crashed since clicking exit.

For Windows no problem - I will try for Linux.

PS: I am happy because I can mac game with -w 1200 -h 660 -windowed ( not app file just game.app/Contents/MacOS/game -w 1200 -h 660 -windowed. It works fine with command line arguments.

But It is so sad because crashing after clicking to exit.

Can you post the stack trace of the crash?

What do you mean? I don’t see where is stack trace?

Sorry I am new for Mac OS 10.12 :confused: