1.不带参数的
Action action = () => {
while (true)
{
//System.Threading.Thread.Sleep(10000);
int bytesRead;
byte[] buffer = new byte[1024];
NetworkStream stream = tc.GetStream();
bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead != 0)
{
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine(data);
}
}
};
action.BeginInvoke( null, null);
2.带参数的
Action<TcpClient> action = new Action<TcpClient>(xianshi);
action.BeginInvoke(tc, null, null);
static void xianshi(TcpClient tc)
{
while (true)
{
System.Threading.Thread.Sleep(10000);
int bytesRead;
byte[] buffer = new byte[1024];
NetworkStream stream = tc.GetStream();
bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead != 0)
{
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine(data);
}
}
}