I am trying to make Udp socket
that sends to specific IP and Port without using Thread
.
When i start Debug, UI Freeze. Start Button doesn't change to stop.
Is there any solution?
private void start_button_click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(ip_textbox.Text) || string.IsNullOrEmpty(port_textbox.Text)) ;
// if textbox empty it doesn't work anything
else
{
if ((string)start_button.Content == "Stop")
// if Start button content is stop, it should stop.
{
start_button.Content = "Start";
}
else
//if Start button Content is Start, it should begin
{
start_button.Content = "Stop";
Data_Sender(ip_textbox.Text, Convert.ToInt32(port_textbox.Text));
}
}
}
private void Data_Sender(string ip, int port)
{
int data_sequence = 1;
// data sequence
byte[] data = new byte[64];
// 64byte data
byte[] byte_data_seq = new byte[4];
// int sequence to byte
byte[] datagram = new byte[1024];
// data + byte_data_seq
List<byte> datagram_list = new List<byte>();
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
while (true)
{
random.NextBytes(data);
byte_data_seq = BitConverter.GetBytes(data_sequence);
datagram_list.AddRange(byte_data_seq);
datagram_list.AddRange(compressed_data);
datagram = datagram_list.ToArray();
datagram_list.Clear();
client.SendTo(datagram, ep);
Thread.Sleep(cycle);
}
}