线程池类错误,求高人

jiaoshoulee jiaoshoulee 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Windows.Forms;


namespace serverchat
{
public class Receiver
{
Socket _socket = null;
NetworkStream _stream = null;
int _index;
Thread _thread = null;
bool _isFree = true;

#region 属性
public Socket Socket
{
get { return _socket; }
set { _socket = value; }
}

public NetworkStream Stream
{
get { return _stream; }
set { _stream = value; }
}

public int Index
{
get { return _index; }
set { _index = value; }
}

public Thread Thread
{
get { return _thread; }
set { _thread = value; }
}

public bool IsFree
{
get { return _isFree; }
set { _isFree = value; }
}
#endregion

//构造方法
public Receiver()
{
this.Thread = new Thread(new ThreadStart(readData));
this.Thread.Start();

}

//激活此线程
public void active(Socket socket, int index)
{
this.IsFree = false;
this.Socket = socket;
this.Index = index;
this.Stream = new NetworkStream(this.Socket);
this.Thread.Resume();
}

//读取数据
public void readData()
{
lock (this)
{
byte[] by=new byte[1024];
int acture = 0;
string content = null;
while (true)
{
try
{
if (this.IsFree)
{
this.Thread.Suspend();
}
else
{
acture = this.Stream.Read(by, 0, by.Length);
content += Encoding.UTF8.GetString(by, 0, acture);

if (!this.Stream.DataAvailable)
{

Pool.sendText(System.Text.Encoding.UTF8.GetBytes(content));
Pool.appendText(content);
content = null;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
this.IsFree = true;
this.Stream = null;
this.Socket = null;
throw;

}
}
}
}
}
}


此类当建立的socket断线了,则报错,报错后为什么线程没挂起来呢?
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
jiaoshoulee
C# 111,129 成员 642,539 内容