コード例 #1
0
        private async void buttonRecive_Click(object sender, System.EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxIP.Text) && !string.IsNullOrEmpty(textBoxPort.Text))
            {
                Ip = textBoxIP.Text;
                //проверка что порт введен коректно
                try
                {
                    Port = Convert.ToInt32(textBoxPort.Text);
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message, "MyError 01", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                //если с портом все ок
                if (Port != 0 && Ip != " ")
                {
                    Task <string> task1 = new Task <string>(() => clientUDP.Listener(Ip, Port));
                    task1.Start();
                    await  task1;
                    string result = task1.Result;

                    textBox1.Text += result + Environment.NewLine;
                }
                else
                {
                    MessageBox.Show("Что то пошло не так с входящими данными", "MyError02", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }