繁体   English   中英

为什么当我在另一个表单上使用 Show 方法时,window 表单卡住并且无法打开

[英]Why when I'm using Show method on another form the window form stuck and won't open

我在哪里打开另一种形式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataDemoLogin
{

public partial class Form2 : Form
{
    Form1 game;

    int portNo = 1500;
    private string ipAddress = "127.0.0.1";
    TcpClient client; //client Socket
    byte[] data; //store the data that send to & from the server
    private string key = "rectangle";

    public Form2()
    {
        InitializeComponent();

        client = new TcpClient();
        client.Connect(ipAddress, portNo);

        data = new byte[client.ReceiveBufferSize];
        // continue reading
        client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(client.ReceiveBufferSize), ReceiveMessage, null);
    }

    private void ReceiveMessage(IAsyncResult ar)
    {
        try
        {
            int bytesRead;

            // read the data from the server
            bytesRead = client.GetStream().EndRead(ar);

            if ((bytesRead >= 1))
            {
                // invoke the delegate to display the recived data
                string incomingData = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
                incomingData = decrypt(incomingData);

                if (incomingData == "loginok" || incomingData == "loginnotok" || incomingData == "registok" || incomingData == "regist not ok")
                {
                    MessageBox.Show(incomingData);

                    if (incomingData == "loginok")
                    {
                        game = new Form1();
                        game.Show();
                        SendMessage("look");
                    }
                }

                /*if (incomingData == "waiting" || incomingData == "connecting")
                {
                    game.recievePrivateMessagaes(incomingData);
                }*/
            }

            // continue reading
            client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(client.ReceiveBufferSize), ReceiveMessage, null);
        }

        catch (Exception ex)
        {
            // ignore the error... fired when the user loggs off
        }
    } // end ReceiveMessage

    /// <summary>
    /// convert the message to ASCII code send message to the server
    /// </summary>
    /// <param name="message">the data to send</param>
    private void SendMessage(string message)
    {
        try
        {
            if (message.StartsWith("%%%%login%%%"))
            {
                message = "%%%%login%%%" + encrypt(message.Remove(0, 12) , key);
            }

            else if (message.StartsWith("%%%regist%%%"))
            {
                message = "%%%regist%%%" + encrypt(message.Remove(0, 12), key);
            }

            else
            {
                message = encrypt(message, key);
            }

            // send message to the server
            NetworkStream ns = client.GetStream();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

            // send the text
            ns.Write(data, 0, data.Length);
            //ns.Flush();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    } //end SendMessage

    private void registbtn_Click(object sender, EventArgs e)
    {
        string username = txbusername.Text;
        string password = txbpassword.Text;
        string fname = txbfname.Text;
        string lname = txblname.Text;
        string email = txbemail.Text;
        string city = txbcity.Text; 

        //1 //sql statement
        SendMessage("%%%regist%%%" + username + "9" + password + "9" + fname + "9" + lname + "9" + email + "9" + city);
    }

    private void loginbtn_Click(object sender, EventArgs e)
    {
        string username = txbuserlogin.Text;
        string password = txbpasslogin.Text;

        //1 //sql statement
        SendMessage("%%%%login%%%" + username + "9" + password);
    }

我试图打开的表格:

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

namespace DataDemoLogin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int Speed = 5;
        private string facing = "right";
        private int coinsCollected = 0;

        private void Keyisdown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                timer2.Enabled = true;
                facing = "left";
                player.Image = Properties.Resources.left;
            }

            if (e.KeyCode == Keys.Right)
            {
                timer2.Enabled = true;
                facing = "right";
                player.Image = Properties.Resources.right;
            }

            if (e.KeyCode == Keys.Down)
            {
                timer2.Enabled = true;
                facing = "down";
                player.Image = Properties.Resources.down;
            }

            if (e.KeyCode == Keys.Up)
            {
                timer2.Enabled = true;
                facing = "up";
                player.Image = Properties.Resources.up;
            }

            if (e.KeyCode == Keys.Left)
            {
                timer2.Enabled = true;
                facing = "left";
            }

            if (e.KeyCode == Keys.Right)
            {
                timer2.Enabled = true;
                facing = "right";
            }

            if (e.KeyCode == Keys.Down)
            {
                timer2.Enabled = true;
                facing = "down";
            }

            if (e.KeyCode == Keys.Up)
            {
                timer2.Enabled = true;
                facing = "up";
            }
        }

    private void gameEngine(object sender, EventArgs e)
    {
        if (facing == "right")
        {
            player.Left += Speed;
        }

        if (facing == "left")
        {
            player.Left -= Speed;
        }

        if (facing == "up")
        {
            player.Top -= Speed;
        }

        if (facing == "down")
        {
            player.Top += Speed;
        }

        if (player.Left < -10)
        {
            player.Left = 590;
        }

        if (player.Left > 600)
        {
            player.Left = 10;
        }

        if (player.Top < -10)
        {
            player.Top = 400;
        }

        if (player.Top > 410)
        {
            player.Top = 10;
        }

        foreach (Control x in this.Controls)
        {
            if ((string)x.Tag == "wall")
            {
                if (player.Bounds.IntersectsWith(x.Bounds))
                {
                    gameOverPlayer("wall");
                }
            }
            else if ((string)x.Tag == "coin")
            {
                if (player.Bounds.IntersectsWith(x.Bounds) && x.Visible)
                {
                    coinsCollected++;
                    x.Visible = false;
                }
            }
        }

        if (coinsCollected == 102)
        {
            gameOverPlayer("coins");
        }
    }

    private void gameOverPlayer(string message)
    {
        timer2.Stop();
    }

    /*public void recievePrivateMessagaes(string message)
    {
        if (message == "waiting" || message == "connected")
        {
            MessageBox.Show(message);
        }
    }*/
}
}

当表单打开时,它会卡住并显示:

表格显示的内容

当我使用 ShowDialog() 方法时,前一个表单仍在工作,但另一个表单不工作,表单打开但前一个表单停止工作。

我没有收到任何错误或其他东西,唯一的事情是当我最小化表单时,我无法重新打开它。

您不能在非 GUI 线程上打开Form (或任何从Control派生的东西)。

这是具有正确 COM 行为和消息泵送的线程,正如您将通过属性和默认情况下在Main中启动主窗体的方式看到的那样。

这不是错误。

您可以使用Invoke (阻塞)或BeginInvoke (非阻塞)编组到 UI 线程。

如果您想检查您是否在 GUI 线程上,请使用InvokeRequired 它将在您当前尝试显示表单时返回 true,表示您需要编组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM