本质上,这些之间的区别是什么?我什么时候应该使用哪种形式?
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();
}
}