ASP.NET C# B⼀S结构 我想实现一个线程监控

我想实现一个简单的线程监控,就是客户端登陆的时候,我吧他的登陆时间记录在数据库,离开的时候(直接关闭页面,或者断电)我记录它的离开时间!我不知道怎么写?从网上看了下 捣鼓了一点点,不知道对不对,而且不知道怎么写下去了,希望大家能帮个忙个修改并完善下,谢谢了!using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Net;using System.Net.Sockets;using System.IO;using JH.Game.ServiceLibrary.Model;using JH.Game.ServiceLibrary.Bll;using FluorineFx.Data;using FluorineFx;using FluorineFx.AMF3;namespace JH.Game.ServiceLibrary.RemotingService{ [RemotingService] public class listener { public listener() { } //IP地址 private IPAddress ip; //端口 private int port; //监听器 private TcpListener Tcls = null; //监听线程 private Thread th; //启动线程 private void Start() { th = new Thread(new ThreadStart(Func)); th.IsBackground=true; th.Start(); } //停止线程 private void Stop() { th.Abort(); if (Tcls != null) { Tcls.Stop(); Tcls = null; } } private void Func() { Tcls = new TcpListener(ip,port); Tcls.Start(); while (true) { if (Tcls.Pending()) { TcpClient tc = Tcls.AcceptTcpClient(); Client cl = new Client(tc, th); th.Start(cl); } else { Thread.Sleep(300); } } }可以加分的!
2026年09月26日 09:47
有3个网友回答
网友(1):

B/S的不用专门一个线程来监控吧
在登录的时候会有一个登录的方法吧,在那里添加登录的记录
然后添加一个全局应用程序类:Global.asax,在里面有个Session_End的事件,这个事件在用户关闭页面之后就会触发,在这里记录他离开的时间,在记录前要先判断下他有没有登录,因为ibuguan有没有登录都会触发这个事件的
至于在他点击退出登录后要不要记录就看你自己了

网友(2):

用socket,服务端上有socket disconnect方法,就是响应断网的或者下线的时的事件。
纪录事件在这方法上完成即可

网友(3):

不懂线程。。关注一下