Zawieszający się klinet

0

Witam

Chce napisać program który wysyła do serwera zapytanie co 1 sekunde a ten odpowiada, na podstawie informacji w internecie udało mi się napisać kod lecz program mi się lekko zawiesza no i niedziała.

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace testwa
{
    public partial class Form1 : Form
    {
      
        TcpClient tcpclnt = new TcpClient();
        Timer timer = new Timer();
        
        byte[] BuforOut = new byte[] { 0x03, 0x03, 0x00, 0x00, 0x00, 0x0a, 0xc4, 0x2f };
        byte[] BuforIn = new byte[50];

       

        public Form1()
        {
            InitializeComponent();
            tcpclnt.ReceiveTimeout = 1000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = 1000;
            
        }
         

        private void button1_Click(object sender, EventArgs e)//start
        {
            button1.Enabled=false;
            timer.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e) //stop
        {
            timer.Enabled = false;
            if (tcpclnt.Connected) tcpclnt.Close();
            this.Close();
        }
        void timer_Tick(object sender, EventArgs e)
        {
          
           try
           {
               if (!(tcpclnt.Connected))   tcpclnt.Connect("192.168.0.100", 501);

               Stream str = tcpclnt.GetStream();
            
               str.Write(BuforOut, 0, BuforOut.Length);
               //System.Thread.Sleep(30);
               str.Read(BuforIn, 0, BuforIn.Length);


               textBox11.Text = Convert.ToString(BuforIn[0])+","+Convert.ToString(BuforIn[1])+","+Convert.ToString(BuforIn[2])+","+Convert.ToString(BuforIn[3])+","+ Convert.ToString(BuforIn[4])+","+Convert.ToString(BuforIn[5]);

               

               str.Close(); 
            }
            catch (Exception ee)
           {
               textBox10.Text = ee.Message;
               textBox1.Text = "0";
               textBox2.Text = "0";
               textBox3.Text = "0";
               textBox4.Text = "0";
               textBox5.Text = "0";
               textBox6.Text = "0";
               textBox7.Text = "0";
               textBox8.Text = "0";
               pictureBox1.BackColor = Color.Red;
               pictureBox2.BackColor = Color.Red;

           }
        
            
         }

      
     
       }

    }
1

Próbowałeś wykonywać to w osobnym wątku?

How to: Create and Terminate Threads (C# Programming Guide)

0

Albo użyj socketów asynchronicznych.

0
Utermiko napisał(a):

Albo użyj socketów asynchronicznych.

Gdzie mogę zobaczyć przykład soketów asynchronicznych??
Po jakim czasie od wysłania mogę oczekiwać odpowiedzi,czy nie ma do tego flagi ??

0

Jeśli dobrze rozumie to po wykonaniu "Connect" metoda nie rzuca wyjatku to znaczy że łączy się z serwerem, a to że przyciski nie działają na form to dlatego że ta aplikacja musi być wielowątkowa,tzn jeden wątek do odczytu danych a drugi do obsługi form??

0

Próbuje zrobić timer który jest wywołany w innym wątku.

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

namespace testwa
{
    public partial class Form1 : Form
    {
        bool flaga = false;

   
        public Form1()
        {

            System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(ProcessTimerEvent), null, 0, 1000);
            InitializeComponent();
          
         
        }

        void ProcessTimerEvent(object obj)
        {
            if (!flaga) { pictureBox2.BackColor = Color.Lime; flaga = false; }
            else { pictureBox2.BackColor = Color.Red; flaga = true; }
            textBox1.Text="ghhhhhhhhhh";
            //while (true) ;
        }

        
     
       }

    }

Niestety timer nie działa form uruchamia się i zaraz znika,gdzie tkwi błąd??
Po wywaleniu textBoxa, działa.

1 użytkowników online, w tym zalogowanych: 0, gości: 1