2015-12-28 2 views
0

Описание:BackgroundWorker и розетки

план был сделать простой многопользовательский понг в выходные дни. Потребовалось 4 часа, чтобы закодировать/разработать физику игры & UI. Провел целый день на этом коде, но не смог обнаружить/исправить его проблему.


Проблема:

Не удается выяснить, почему он не работает, он застревает на 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 предназначен для работы с ЦП. Вы выполняете работу с IO. У вас не должно быть BGW. – Servy

+0

Спасибо, Что я должен использовать? Темы тоже не работали. –

+0

Просто используйте асинхронные методы 'Socket'. – Servy

ответ

0

Просто использовать асинхронные методы Socket. –   Сервис

Большое спасибо, вот что я искал. - Varand Vartanian

Смежные вопросы