2015-12-28 19 views
0

說明:BackgroundWorker的和插口

計劃是製作一個簡單的多人乒乓球比賽在一個週末。 花了4個小時來編寫/設計遊戲物理&用戶界面。 在此代碼上花了一整天的時間,但無法檢測/修復它的問題。


問題:

不能找出爲什麼它不工作,它就會被卡住int bytesRead = accepted.Receive(Buffer1);Reveicva bgWorker而Senda bgWorker不運行。

下面是代碼:

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.Windows.Forms; 
using System.Net; 
using System.Net.Sockets; 

namespace LePong 
{ 
    public partial class Form1 : Form 
    { 
     int speedTop = 3, speedLeft = 5; 
     int playerRight = 0, playerLeft = 0; 
     bool leRuler = true; 
     static byte[] Buffer1 { get; set; } 
     public int rckt2Pos; 

     public Form1() 
     { 
      InitializeComponent(); 

      //Sets Cursor at Top of Racket 
      Point p = new Point(racket1Main.Left/2, racket1Top.Bottom); 
      Cursor.Position = p; 
      Cursor.Hide(); 
       Receivea.RunWorkerAsync(); 
        Senda.RunWorkerAsync(); 

     } 

     private void Receivea_DoWork(object sender, DoWorkEventArgs e) 
     { 

      Socket sckt1; 
      sckt1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      sckt1.Bind(new IPEndPoint(0, 1234)); 
      Socket accepted; 
      while (true) 
      { 
       if (leRuler == true) 
       { 
        sckt1.Listen(100); 
        accepted = sckt1.Accept(); 
        Buffer1 = new byte[accepted.SendBufferSize]; 
        int bytesRead = accepted.Receive(Buffer1); 
        string strData; 

        byte[] formatted = new byte[bytesRead]; 

        for (int i = 0; i < bytesRead; i++) 
        { 
         formatted[i] = Buffer1[i]; 
        } 

        strData = Encoding.ASCII.GetString(formatted); 
        try 
        { 
         racket2Main.Top = int.Parse(strData); 
         leRuler = false; 
        } 
        catch { } 
       } 
      } 
     } 

     private void Senda_DoWork(object sender, DoWorkEventArgs e) 
     { 

      Socket sckt2; 
      string text; 

      sckt2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234); 
      try 
      { 
       sckt2.Connect("127.0.0.1", 1234); 
      } 
      catch { } 
      while (true) 
      { 
       if (leRuler == false) 
       { 
        text = Cursor.Position.Y + ""; 
        byte[] data = Encoding.ASCII.GetBytes(text); 
        sckt2.Send(data); 
        leRuler = true; 
       } 
      } 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      if (Receivea.IsBusy == false) 
       Receivea.RunWorkerAsync(); 
      if (Senda.IsBusy == false) 
       Senda.RunWorkerAsync(); 
      //Sets Racket1 Postion 
      racket1Main.Top = Cursor.Position.Y; 
      racket1Back.Top = racket1Top.Top; 
      racket1Top.Top = racket1Main.Top - racket1Top.Height; 
      racket1Bottom.Top = racket1Main.Bottom; 
      //Sets Racket2 Postion On Screen 
      racket2Top.Left = playground.Right - racket1Top.Width - 12; 
      racket2Main.Left = playground.Right - racket1Main.Width - 12; 
      racket2Bottom.Left = playground.Right - racket1Main.Width - 12; 
      racket2Back.Left = playground.Right - racket1Back.Width - 12; 
      //Sets Racket2 Postion 
      //racket2Main.Top = Cursor.Position.Y; 
      racket2Back.Top = racket2Top.Top; 
      racket2Top.Top = racket2Main.Top - racket2Top.Height; 
      racket2Bottom.Top = racket2Main.Bottom; 
      playerLeftScore.Left = racket2Main.Left + 3; 
      //Sets Scores On Rackets 
      if (playerLeft > 0) 
       playerLeftScore.Text = "" + playerLeft; 
      if (playerRight > 0) 
       playerRightScore.Text = "" + playerRight; 
      playerLeftScore.Top = racket2Main.Top + 25; 
      playerRightScore.Top = racket1Main.Top + 25; 
      //Sets Ball Speed/Postion 
      Ball.Top += speedTop; 
      Ball.Left += speedLeft; 
      //Checks If Ball Hits Screen Bounds 
      if (Ball.Top <= playground.Top) { speedTop = -speedTop; } 
      if (Ball.Bottom >= playground.Bottom) { speedTop = -speedTop; } 
      if (Ball.Right >= playground.Right) { playerRight++; speedLeft = -speedLeft; speedTop = -speedTop; Ball.Top = playground.Height/2 - Ball.Height/2; Ball.Left = playground.Width/2 - Ball.Width/2; } 
      if (Ball.Left <= playground.Left) { playerLeft++; speedLeft = -speedLeft; speedTop = -speedTop; Ball.Top = playground.Height/2 - Ball.Height; Ball.Left = playground.Width/2 - Ball.Width/2; } 
      //Checks If Ball Hits Racket1 
      if (Ball.Bounds.IntersectsWith(racket1Top.Bounds)) { speedTop = -3; speedLeft = 5; } 
      if (Ball.Bounds.IntersectsWith(racket1Main.Bounds)) { speedTop = speedTop - speedTop/2; speedLeft = 5; } 
      if (Ball.Bounds.IntersectsWith(racket1Bottom.Bounds)) { speedTop = 3; speedLeft = 5; } 
      //Checks If Ball Hits Racket2 
      if (Ball.Bounds.IntersectsWith(racket2Top.Bounds)) { speedTop = -3; speedLeft = -5; } 
      if (Ball.Bounds.IntersectsWith(racket2Main.Bounds)) { speedTop = speedTop - speedTop/2; speedLeft = -5; } 
      if (Ball.Bounds.IntersectsWith(racket2Bottom.Bounds)) { speedTop = 3; speedLeft = -5; } 





     } 



    } 
} 
+1

BGW用於CPU綁定工作。你在做IO界定工作。你根本不應該有BGW。 – Servy

+0

謝謝, 我應該使用什麼? 線程無法工作。 –

+0

只需使用'Socket'的異步方法即可。 – Servy

回答

0

只需使用的Socket異步方法。 –   Servy

非常感謝你,這就是我一直在尋找的。 - Varand Vartanian