thread.abort java_ThreadAbort无法使用套接字中的readline

我使用线程打开服务器的套接字并以同步方式从服务器读取数据来实现TCP客户端 . 当行 String thisLine = aReadStream.ReadLine(); 阻塞因为没有数据要从套接字读取而我尝试执行 Thread.Abort 来杀死该线程(因为它在该ReadLine()上被阻塞)我希望能够捕获 ThreadAbortException BUT 我可以'吨 .

线程在 ReadLine() 上仍然被阻塞,并且没有被杀死 . 在我的线程中使用的代码下面 .

你知道为什么和我做错了什么以及如何解锁ReadLine()?

private void readSocket_andTriggerEvents()

{

TcpClient aClient = null;

try

{

aClient = new TcpClient();

aClient.Connect(_HOST, _PORT);

Trace.WriteLine("Socket Connected");

NetworkStream aStream = aClient.GetStream();

StreamReader aReadStream = new StreamReader(aStream);

int nTimes = 0;

while (this.isSocketThreadStarted)

{

String thisLine = aReadStream.ReadLine(); // when no data

// is available the application hangs here.

// Thread.Abort doesn't work!

}

}

catch (ThreadAbortException ex)

{

Trace.WriteLine("The Thread was brute-forced killed");

// I never come here!!

}

catch (SocketException ex)

{

Helper.ShowErrorMessage(ex);

}

finally{

aClient.Close();

Trace.WriteLine("socket closed");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值