Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Show()挂起新的表单实例_C# - Fatal编程技术网

C# Show()挂起新的表单实例

C# Show()挂起新的表单实例,c#,C#,因此,我有一个名为Common.cs的类,在这个类中,我有一个函数,它创建一个表单实例,frmMainGame.cs,然后调用一个方法,该方法应该调用这个新表单实例,但由于某些原因,它没有。因此,当使用mainGame.Show()显示表单时表格就挂在那里,我什么都做不了 代码如下: frmMainGame.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Da

因此,我有一个名为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);
    }
}
}

您需要一个消息泵,它是通过将表单实例传递给
应用程序来创建的。Run()


您需要一个消息泵,它是通过将表单实例传递给
应用程序来创建的。Run()


您可能需要重新设计一点类结构。我建议您不要在Common.cs中创建主窗体的实例,而是按照正常的方式操作,即使用
Application.Run()
在program.cs中创建主窗体的实例,以便它为您安装一个消息泵,将套接字通信类保存在一个单独的文件中(可能就是这样)然后在主窗体中实例化它们。如果您必须在多个表单中提供这些通信类,那么继续在program.cs中创建它们的实例,并将它们作为参数传递给不同的表单/其他模块。

您可能需要重新设计一点类结构。我建议您不要在Common.cs中创建主窗体的实例,而是按照正常的方式操作,即使用
Application.Run()
在program.cs中创建主窗体的实例,以便它为您安装一个消息泵,将套接字通信类保存在一个单独的文件中(可能就是这样)然后在主窗体中实例化它们。如果必须在多个窗体上提供这些通信类,则继续在program.cs中创建它们的实例,并将它们作为参数传递给不同的窗体/其他模块。

如果这样做,则无法访问在program中创建的网络类。在类中以这种方式运行任何东西之前,它会一直等到frmMainGame关闭。我同意@dotNET的观点,你需要重新构造你拥有东西的方式。如果涉及表单,则Application.Run()不是可选的。如果在主窗体关闭之前,您的通信无法正常工作,那么您正在做一些非常非常规的事情,我们需要更多详细信息来了解这一点…@dylandtoffics在调用
应用程序之前在另一个线程中启动您的网络代码。运行
我如何将整个类作为一个新线程启动?一个线程指向方法。在该方法中,创建类并“启动”它。你们有并没有尝试过创建一个线程?…若我这样做了,那个么我就无法访问在程序中创建的网络类。在类中以这种方式运行任何东西之前,它会一直等到frmMainGame关闭。我同意@dotNET的观点,你需要重新构造你拥有东西的方式。如果涉及表单,则Application.Run()不是可选的。如果在主窗体关闭之前,您的通信无法正常工作,那么您正在做一些非常非常规的事情,我们需要更多详细信息来了解这一点…@dylandtoffics在调用
应用程序之前在另一个线程中启动您的网络代码。运行
我如何将整个类作为一个新线程启动?一个线程指向方法。在该方法中,创建类并“启动”它。你有没有尝试过创建一个线程呢?。。。
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();
    }

}
}
     Application.Run(new frmMainGame());