rosenberg37 / bombplane Goto Github PK
View Code? Open in Web Editor NEW纸上飞机大战(炸飞机)游戏实现
License: MIT License
纸上飞机大战(炸飞机)游戏实现
License: MIT License
private void Listen(object obj) //建立与客户端的连接
{
CancellationToken ct = (CancellationToken)obj;
while (!ct.IsCancellationRequested) /*!此处问题!*/
{
try
{
......//省略
}
catch (SocketException) { }
}
}
在上面这个函数中,使用了while (!ct.IsCancellationRequested)来判断线程是否被取消。(Server.cs行82)
public int ListenPort
{
get { return _listenPort; }
set
{
try
{
if (listenThread != null)
listenCTS.Cancel(); /*!此处问题!*/
......//省略
//开启线程Accept进行通信的客户端socket
listenThread = new Thread(Listen)
{
IsBackground = true //运行线程在后台执行
}; //线程绑定Listen函数
listenThread.Start(listenCTS.Token); /*!此处问题!*/ //Start里面的参数是Listen函数所需要的参数
_listenPort = value;
}
catch (SocketException)
{
MessageBox.Show("监听程序启动失败,请尝试重新设置监听端口");
}
}
}
但在设置ListenPort属性的时候,调用了listenCTS.Cancel()方法(Server.cs行50),导致ct.IsCancellationRequested变为true。
之后在创建新线程时,仍然使用了旧的listenCTS提供Token(Server.cs行67),导致Listen函数的while循环永远不会执行。
应该在ListenPort属性的set方法的中,调用listenCTS.Cancel()方法后,重新创建一个新的CancellationTokenSource对象,以便生成一个新的未取消的token。
public class Server
{
......//省略
private readonly CancellationTokenSource? listenCTS;
......//省略
}
但在原代码中,使用了readonly修饰listenCTS(Server.cs行38),因此我不确定将 readonly
删去,之后再粗暴地添加 listenCTS.Dispose()
和 listenCTS = new CancellationTokenSource()
是不是一个好的做法,不过确实是可行的。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.