如何在visual studio上运行两个进程

时间:2015-04-30 16:43:52

标签: c# visual-studio

我正在XNA中编写一个游戏,我有登录屏幕,它是windows窗体,还有游戏本身。我需要从登录界面进入游戏,但是当我尝试它时说我当时不能再跑一次。我怎么能解决这个问题? 这是登录屏幕代码:

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 ProtoType
{
    public partial class SighIn : Form
    {
        public SighIn()
     {
        InitializeComponent();

     }

    private void button1_Click(object sender, EventArgs e)
    {
        if ((textBox1.Text.Equals("Developer")) && (textBox2.Text.Equals("poxus17")))
        {
            using (Game1 game = new Game1())
            {                  
                game.Run();
            }

        }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

XNA Game.Run方法执行Application.Run,​​它为主线程(UI线程)提供消息泵。

在表单运行并单击按钮的时间点,Application.Run已在执行(可能通过Form.ShowDialog)。您不能在同一个线程中同时拥有两个消息泵。

解决方案是允许Application.Run完成,然后调用Game.Run。

这样的事情:

Form form = new SignIn();
if (form.ShowDialog() == DialogResult.OK)
{
    if (form.UserName =="Developer" && form.Password == "poxus17")
    {
        using (Game1 game = new Game1())
        {
            game.Run();
        }
    }
}

现在,您的表单的按钮单击处理程序可以将文本框字段复制到属性(UserName和Password)并设置this.DialogResult = DialogResult.OK。这将关闭表单,完成ShowDialog启动的消息泵,然后在验证后,使用Game.Run启动一个新的消息泵。