FrankT
June 26th, 2008, 02:38 PM
Hi folks. I'm new to C# so please bear with me. I have a UdpClient application that uses UdpClient.Receive. This works ok but I've been trying to get BeginReceive to work. I've read countless examples and I still don't get it. Could someone please explain very very simply how to use BeginReceive.
Thanks in advance for your time
Frankt
Talikag
June 26th, 2008, 03:11 PM
As you probably know, the Receive method blocks, i.e. the statements after it will be executed only after the message will be received (or the timeout will expire).
Well, in most cases we will not want such thing. We want to recieve the message, and in the meantime, do other progresses.
One solution for this problem will be creating a thread, and receive there the message. However, it's not very comfortable.
Another, and probably easier solution will be to use the "asynchronous communication" mechanism. The BeginRecieve is part of it. This mechanism allows us to do what we did with the thread, but we'll only have to create a method - we will not have to create the thread.
Behind the scenes, the computer will create a thread, and call the method you defined as the recieve method (one of the parameters BeginReceive gets is the name of this method) there.