3
基本上,這些超越了顯而易見的區別是什麼?我應該什麼時候使用哪種形式?什麼時候應該使用UdpClient.BeginReceive?什麼時候應該在後臺線程上使用UdpClient.Receive?
class What
{
public Go()
{
Thread thread = new Thread(new ThreadStart(Go2));
thread.Background = true;
thread.Start();
}
private Go2()
{
using UdpClient client = new UdpClient(blabla)
{
while (stuff)
{
client.Receive(guh);
DoStuff(guh);
}
}
}
}
與
class Whut
{
UdpClient client;
public Go()
{
client = new UdpClient(blabla);
client.BeginReceive(guh, new AsyncCallback(Go2), null);
}
private Go2(IAsyncResult ar)
{
client.EndReceive(guh, ar);
DoStuff(guh);
if (stuff) client.BeginReceive(guh, new AsyncCallback(Go2), null);
else client.Close();
}
}