Я подключен к TcpClient
с помощью Thread
. Теперь соединение TcpClient правильно открывается в потоке, но закрытие TcpClient и Thread происходит неправильно. Я не знаю, почему ..Как закрыть TcpClient, открытый в потоке должным образом
Вот эта тема, которую я начинаю здесь.
private System.Threading.Thread _thread;
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
_thread = new Thread(DoWork);
_thread.Start();
и вот соединение TcpClient ..
private void DoWork()
{
while (!_shutdownEvent.WaitOne(0))
{
try
{
client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse(ip),intport));
//Say thread to sleep for 1 secs.
Thread.Sleep(1000);
}
catch (Exception ex)
{
// Log the error here.
client.Close();
continue;
}
try
{
using (stream = client.GetStream())
{
byte[] notify = Encoding.ASCII.GetBytes("Hello");
stream.Write(notify, 0, notify.Length);
byte[] data = new byte[1024];
while (!_shutdownEvent.WaitOne(0))
{
int numBytesRead = stream.Read(data, 0, data.Length);
if (numBytesRead > 0)
{
line= Encoding.ASCII.GetString(data, 0, numBytesRead);
}
}
...
и теперь вот код, чтобы закрыть и resstart Нить и TcpClient ..
_shutdownEvent.WaitOne(0);
_thread.Abort();
//Start Again
_thread = new Thread(DoWork);
_thread.Start();
Пожалуйста, помогите мне, чтобы остановить и запустите Thread и TcpClient правильно. Спасибо ..
Это так же, как, вероятно, чтобы убить новую нить :) –
Я не могу перезапустить 'TcpClient();' клиент с данным кодом –
'_thread = новая тема (DoWork); _thread.Start(); 'с этим кодом я не могу получить точку останова в методе Dowork(). –