private static void doSomething(IAsyncResult result) { System.Threading.Thread newThread = new System.Threading.Thread((System.Threading.ThreadStart)delegate { //Do whatever you want in a new thread var ss = cli.Connected; Console.WriteLine("Connected " +ss ); while (!cli.Connected) { using (NetworkStream networkStream = cli.GetStream()) { if (networkStream.CanRead) { // Reads NetworkStream into a byte buffer. byte[] bytes = new byte[cli.ReceiveBufferSize]; // Read can return anything from 0 to numBytesToRead. // This method blocks until at least one byte is read. networkStream.Read(bytes, 0, (int)cli.ReceiveBufferSize); // Returns the data received from the host to the console. string returndata = Encoding.UTF8.GetString(bytes); Console.WriteLine("This is what the host returned to you: " + returndata); } else { Console.WriteLine("You cannot read data from this stream."); cli.Close(); // Closing the tcpClient instance does not close the network stream. networkStream.Close(); return; } networkStream.Close(); //if (networkStream.CanWrite) //{ // Byte[] sendBytes = Encoding.UTF8.GetBytes("Is anybody there?"); // networkStream.Write(sendBytes, 0, sendBytes.Length); //} //else //{ // Console.WriteLine("You cannot write data to this stream."); // cli.Close(); // // Closing the tcpClient instance does not close the network stream. // networkStream.Close(); // return; //} } } }); newThread.Start(); //Start executing the code inside the thread //This code will still run while the newThread is running Console.ReadLine(); //Wait for user input newThread.Abort(); //Stop the thread when the user inserts any thing } private static void send(string msg) { AsyncCallback callBack = DoOtherOption; //Set the callback to the doSomething void cli.BeginConnect("10.10.10.10.", 23, callBack, cli); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)