我有这个代码,我感觉不到它应该工作得很好。我已将它拼凑在一起并且可以正常工作,但我并不完全了解异步线程和ResetEvents。
当我使用多线程客户端测试此侦听器时,在响应时间内发送100个连接,前10个时间为300毫秒,最后50个时间为2-3秒。
1。)线程和重新发送处理程序是否按照它们的设计实现?
2。)是否有某种方法可以加快高流量突发的响应时间?
3。)在另一个线程上运行tcpConnectionLogic是否有意义?
我有一个全局范围的手动和自动重置事件:
private AutoResetEvent connectionWaitHandle = new AutoResetEvent(false);
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
我有一个Windows服务,它在新线程上启动tcp侦听器
protected override void OnStart(string[] args)
{
_mainThread = new Thread(ThreadListener_Begin);
_mainThread.Name = "EMServiceThread";
_mainThread.IsBackground = false;
_mainThread.Start();
}
我的线程运行一个循环,等待手动重发事件发出关闭信号。
private void ThreadListener_Begin()
{
TcpListener listener = null;
listener = new TcpListener(IPAddress.Parse("172.16.30.248"), 10010);
listener.ExclusiveAddressUse = false;
listener.Start();
while (!_shutdownEvent.WaitOne(0))
{
IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
connectionWaitHandle.WaitOne();
}
}
最后,当一个连接进来时,我接受它并将其传递给一种处理请求的逻辑层。
private void HandleAsyncConnection(IAsyncResult result)
{
TcpListener listener = (TcpListener)result.AsyncState;
connectionWaitHandle.Set();
TcpClient c = listener.EndAcceptTcpClient(result);
var _tcpConnectedLogic = new TcpConnectionLogic(c);
_tcpConnectedLogic.BadRequestAlert += _serviceLogic_BadRequestAlert;
_tcpConnectedLogic.RequestDecodedAlert += _tcpConnectedLogic_RequestDecodedAlert;
_tcpConnectedLogic.Start();
}
修改 当我停止服务时,我的线程没有像我期望的那样关闭。相反,它结束了堕胎。为什么?我怎样才能优雅地接近它?
protected override void OnStop()
{
WriteToEventLog("Stop Command Received" + CurrentTimeStamp, entryType: EventLogEntryType.Information);
WriteToLogFile("Stop Command Received @ " + CurrentTimeStamp);
_shutdownEvent.Set();
if (!_mainThread.Join(4000))
{
WriteToEventLog("OnStop: Aborting thread, Join did not work. This is not preferred.", entryType: EventLogEntryType.Warning);
_mainThread.Abort();
}
}
答案 0 :(得分:2)
在这个块中
while (!_shutdownEvent.WaitOne(0))
{
IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
connectionWaitHandle.WaitOne();
}
您基本上是在循环中接受客户端,因为您通过等待connectionWaitHandle来阻止线程。你也可以忘记异步的东西而且只做:
while (!_shutdownEvent.WaitOne(0))
{
var client = listener.AcceptTcpClient();
// start your logic in separate thread
}
我不是说你应该这样做,只要注意你当前的" async"实现不会比同步实现任何好处。
然后,由于对connectionWaitHandle的阻塞相同,你遇到了中止线程的问题。例如,假设你只是启动你的监听器而没有连接任何客户端。然后客户端在connectionWaitHandle上被阻止,你设置_shutdownEvent的事实没有帮助 - 主线程永远不会有机会检查它。
解决问题的最佳方法是使用基于任务的方法:
class Listener {
// we don't use cancellation tokens because AcceptTcpClientAsync does not support them anyway
private volatile bool _shutdown;
private TcpListener _listener;
public async Task Start() {
_listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 10010);
_listener.ExclusiveAddressUse = false;
_listener.Start();
while (!_shutdown) {
TcpClient client = null;
try {
client = await _listener.AcceptTcpClientAsync();
}
catch (ObjectDisposedException) {
// will be thrown when you stop listener
}
if (client != null) {
var tcpConnectedLogic = new TcpConnectionLogic(client);
// start processing on thread pool thread if necessary
// don't wait for it to finish - you need to accept more connections
tcpConnectedLogic.StartAsync();
}
}
}
public void Stop() {
_shutdown = true;
_listener.Stop();
}
}