Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a demo(Demoscene) using XNA, but i want a window to pop out first, where you can select your settings. But when i code a button to Open a new Game1() instance, i get this(InvalidOperationException):

Det er ikke en gyldig operasjon å starte en ny meldingsløkke i en enkelt tråd. Bruk Form.ShowDialog i stedet.

Which translates in english to It is not a valid operation to start a new messageloop(?) in a single thread. use Form.-ShowDIalog instead.

I am using showDialog for my settings form. What needs change?

Source:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tg12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Text = "Start!";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Game1 game = new Game1();
            game.Run();
        }
    }
}

Program.cs:
C#
using System;

namespace Tg12
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            Form1 form = new Form1();
            form.ShowDialog();
        }
    }
#endif
}
Posted
Updated 21-Dec-11 5:39am
v2

1 solution

Well I am not sure what you are doing is a good idea but the only way I can see to do this right now in my tired state of mind is to start a new thread. i.e.

C#
private void button1_Click(object sender, EventArgs e)
{
    System.Threading.Thread thStartGame = new System.Threading.Thread(StartGame);
    thStartGame.Start();
}

private void StartGame()
{
    Game1 game = new Game1();
    game.Run();
}
 
Share this answer
 
v2
Comments
Liam S. Crouch 21-Dec-11 11:56am    
I get
The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)' has some invalid arguments

and Cannot convert from 'void' to 'System.Threadind.parameterizedThreadStart'
LanFanNinja 21-Dec-11 12:57pm    
Show me your code.
Liam S. Crouch 21-Dec-11 13:01pm    
Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System;
using System.Threading;

namespace Tg12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Text = "Start!";
}

private void button1_Click(object sender, EventArgs e)
{
Game1 ogame = new Game1();
Thread theThread = new Thread(new ThreadStart(StartGame()));
}
public void StartGame()
{
Game1 game = new Game1();
game.Run();
}
}
}

Program.cs

using System;

namespace Tg12
{
#if WINDOWS || XBOX
static class Program
{
///
/// The main entry point for the application.
///

static void Main(string[] args)
{
Form1 form = new Form1();
form.ShowDialog();
}
}
#endif
}
LanFanNinja 21-Dec-11 13:42pm    
Use this code exactly as it is written.

private void button1_Click(object sender, EventArgs e)
{
//Game1 ogame = new Game1();

Thread theThread = new Thread(StartGame);
// OR
//Thread theThread = new Thread(new ThreadStart(StartGame));

theThread.Start();
}

public void StartGame()
{
Game1 game = new Game1();
game.Run();
}
LanFanNinja 21-Dec-11 13:45pm    
Note the lack of parenthesis from StartGame.

Your code: Thread theThread = new Thread(new ThreadStart(StartGame()));

My code: Thread theThread = new Thread(new ThreadStart(StartGame));
or
My code: Thread theThread = new Thread(StartGame);

EDIT:
Also in your button1_Click event handler you are creating an instance of Game1 called "ogame" this is not needed an instance of Game1 is being created in the StartGame() method.

In order for you to be able to create "ogame" in button1_Click and the run it in StartGame() would require more work as there would be a cross thread error.

If you need access to the game instance outside of StartGame() you will need to make it global. i.e. something like this:

public partial class Form1 : Form
{
Game1 ogame;

public Form1()
{
InitializeComponent();
button1.Text = "Start!";
}

private void button1_Click(object sender, EventArgs e)
{
Thread theThread = new Thread(StartGame);
theThread.Start();
}

public void StartGame()
{
ogame = new Game1();
ogame.Run();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900