gpt4 book ai didi

c# - 试图中断卡在 reader.readLine() 中的线程

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:31 27 4
gpt4 key购买 nike

我通过 TCP 与 IRC 服务器建立了连接。到目前为止,我用独立任务读取数据,一切顺利。但是,如果我想退出程序,我无法退出线程,因为它卡在 reader.ReadLine() 命令中(threadShouldRun 没有影响)。使用 Interrupt() 或 Abort() 似乎也没有改变任何东西。

要么我需要一种方法来确定何时有更多行要读取,要么我需要强行终止线程(即使那很糟糕)。

private System.Threading.Thread myThread;
private bool threadShouldRun = true;
private StreamReader reader;

private void readStream()
{
while(threadShouldRun)
{
string line = reader.ReadLine();
if (line != null)
{
newLineEvent(this, new NewLineEventArgs(line));
}
}
}

最佳答案

使用此处所示的 BeginRead 之类的异步调用并避免循环:

http://msdn.microsoft.com/en-us/library/system.io.stream.beginread.aspx

关于c# - 试图中断卡在 reader.readLine() 中的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com