所以我在这个类中有一个名为 Common.cs 的类我有一个函数可以创建一个表单实例 frmMainGame.cs ,然后调用一个方法应该调用这个新的表单实例,但由于某种原因,它不会。因此,当使用 mainGame.Show(); 显示表单时,表单就会挂起,我无法对其执行任何操作。
以下是代码:
frmMainGame.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace Ghoul_Engine
{
public partial class frmMainGame : Form
{
public delegate void openGame();
public delegate void refreshGame();
public delegate void updateScreen(Bitmap image);
drawing draw;
public bool isTyping = false;
public Graphics g;
public List<String> Chat = new List<String>();
public List<String> chat
{
get { return Chat; }
}
public frmMainGame()
{
draw = new drawing(this);
InitializeComponent();
}
public Size size{
get { return this.Size; }
}
public void showGame()
{
if (InvokeRequired)
{
MessageBox.Show("Invoke");
BeginInvoke(new openGame(showGame), null);
}
else
{
this.Show();
this.Focus();
}
}
public void refresh()
{
if (this.InvokeRequired)
{
this.Invoke(new refreshGame(refresh), null);
}
else
{
this.Refresh();
}
}
private void frmMainGame_Load(object sender, EventArgs e)
{
this.DoubleBuffered = true;
draw.drawText(new string[] { "GHOUL Game Engine Alpha V0.0.1" }, 12, 0, 0, Brushes.Yellow);
clientNetHandler handlers = new clientNetHandler();
}
public void update(Bitmap image)
{
if (InvokeRequired)
{
this.Invoke(new updateScreen(update), new object[] { image });
}
else
{
pbScreen.Image = image;
}
}
private void frmMainGame_Close(object sender, FormClosingEventArgs e)
{
Program.Network.Send("logout", Program.netHandlers.getDataByte(clientNetHandler.cNetType.Logout));
//Program.Network.closeSocket();
Program.mainMenu.showMainMenu();
Program.mainGame = new frmMainGame();
}
private void tmrRefreshScreen_Tick(object sender, EventArgs e)
{
}
private void frmMainGame_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void pbScreen_Click(object sender, EventArgs e)
{
draw.drawText(Chat.ToArray(), 12, 0, 30, Brushes.Green);
}
}
}
Common.Cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Drawing;
namespace Ghoul_Engine
{
class Common
{
public Common()
{
}
public void innnitiateGame(String login)
{
Program.mainMenu.hideMainMenu();
frmMainGame mainGame = new frmMainGame();
mainGame.showGame();
}
public void preExit()
{
Program.Network.closeSocket();
}
}
}
答案 0 :(得分:2)
您需要一个消息泵,它是通过将表单实例传递给Application.Run()
来创建的:
Application.Run(new frmMainGame());
答案 1 :(得分:2)
您可能需要稍微重新设计一下类结构。不是在Common.cs中创建主窗体的实例,我建议你按照正常的方式继续,即使用Application.Run()
在program.cs中创建主窗体的实例,以便为你安装一个消息泵,将套接字通信类保存在单独的文件中(可能就像它们一样),然后在主窗体中实例化它们。如果必须在多个表单中提供这些通信类,那么继续在program.cs中创建它们的实例,并将它们作为参数传递给不同的表单/其他模块。