簡體   English   中英

C#:標簽文本和圖片框位置不變

[英]C#: label text and Picturebox Location do not change

我想用tcp編寫多人游戲。 發送方可以工作並發送數據,但是在接收方更改標簽的文本和PictureBox的位置之后,什么也沒有發生。 我調試了它,發現標簽上有新的文本,但是它沒有在窗口中顯示。 我搜索了一下,發現我的主線程被阻塞了。 但是我可以移動第二個玩家。

Multiplayer.cs:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Game
{
class Multiplayer
{
    public string ClientAdress;


    public void StartSession(int port)
    {
        Form1 form1 = new Form1();
        while (true)
        {
            try
            {
                TcpListener listener = new TcpListener(IPAddress.Any, port);
                listener.Start();
                Socket socket = listener.AcceptSocket();
                byte[] b = new byte[100];
                int k = socket.Receive(b);

                string update = null;

                for (int i = 0; i < k; i++)
                {
                    update += Convert.ToChar(b[i]).ToString();
                }
                IPEndPoint remoteIPEndPoint = socket.RemoteEndPoint as IPEndPoint;
                ClientAdress = remoteIPEndPoint.Address.ToString();
                form1.GetData(ClientAdress, update);
                socket.Close();
                listener.Stop();
            }
            catch (Exception ex)
            {
                form1.Exception(ex);
            }
        }
    }

    public void JoinSession(string ipadress, int port)
    {
        Form1 form1 = new Form1();
        {
            try
            {
                TcpClient tcpclnt = new TcpClient();
                tcpclnt.Connect(ipadress, port);

                Stream stm = tcpclnt.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();

                string message = form1.SendData();

                byte[] ba = asen.GetBytes(message);
                stm.Write(ba, 0, ba.Length);
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                tcpclnt.Close();
                form1.senderRunning = false;
            }
            catch (Exception ex)
            {
                form1.Exception(ex);
                form1.senderRunning = false;
            }
        }
    }
}
}

Button3:

        Multiplayer mp = new Multiplayer();
    private void button3_Click(object sender, EventArgs e)
    {
        if (!startjoinmenu)
        {
            Thread Listener = new Thread(delegate () { mp.StartSession(Int32.Parse(textBox2.Text)); });
            if (!ThreadisRunning)
            {
                ThreadisRunning = true;
                Listener.Start();
            }
        }
        else
        {
            if (!senderRunning)
            {
                Thread Sender = new Thread(delegate () { mp.JoinSession(textBox1.Text, Int32.Parse(textBox2.Text)); });
                senderRunning = true;
                Sender.Start();
            }
        }

獲取數據:

public void GetData(string client, string data)
    {
        try
        {
            string dataX;
            string dataY;
            int positionOf = data.IndexOf(";");

            dataX = data.Substring(0, positionOf);
            dataY = data.Substring(positionOf + 1, data.Length - positionOf - 1);
            //enemyplayer.Location = new Point(Int32.Parse(dataX), Int32.Parse(dataY));

            enemyplayer.Left = Int32.Parse(dataX);
            enemyplayer.Top = Int32.Parse(dataY);

            label3.Text = "Connected with: " + client + "!";

        }
        catch (Exception ex)
        {
            Exception(ex);
        }
    }

SendData:

    public string SendData()
    {
        string playerX = player.Location.X.ToString();
        string playerY = player.Location.Y.ToString();
        return playerX + ";" + playerY;
    }

在我看來,您StartSessionStartSession方法中使用了while(true)語句。 這將導致您的主線程被阻塞,因為UI僅在所有方法完成執行后才進行更新(在這種情況下,如果使用while (true)則它永遠不會完成執行該方法)。

要解決此問題,可以使用Application.DoEvents(); 方法。 您可以在此處了解更多信息。 這將告訴應用程序處理其他正在等待執行的事件(例如重新粉刷UI以反映您在代碼中對UI對象所做的更改)。 因此,在您的代碼中,我可能會這樣做:

public void StartSession(int port)
{
    Form1 form1 = new Form1();
    while (true)
    {
        try
        {
            // ...

            form1.GetData(ClientAdress, update);

            // Tell the application to execute other events that are in queue
            Application.DoEvents();

            // ...
        }
        catch (Exception ex)
        {
            form1.Exception(ex);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM