I’m writing a chat over UDP. I faced a problem with a blocking function ReceiveFrom(), when I tried to go from Console Application to Windows Forms. When I try to create a form, and click a button for the listening incoming packets with ReceiveFrom() it’s just blocks the program. I’m writing something like this:
private void Listen_button_click(object sender, EventArgs e)
{
while(true){
ReceiveFrom(buf, ref clientEP);
data = buf.ToData(); //convert from bytes to string.
displayMessageDelegate(data);
packet = new Packet(acknowlegment);//acknowledgment that packet was received.
ack = packet.ToStream();
SendTo(ack, clientEP);//send ack, so client knows everything is ok.
}
I know that I could use TCP, and life would be much more easier, but it’s my task to do this way.
And I know that I could use non-blocking BeginReceiveFrom(), but I want to know if it is possible somehow to listen in WF using blocking function.
I found out that the problem of blocking functions and UI is solved by using async and await keywords. Following example shows the solution: