注册 登录
编程论坛 ASP.NET技术论坛

监控线程的问题!

songgaotong 发布于 2010-12-01 12:37, 531 次点击
我想实现一个简单的线程监控!就是客户端登陆的时候,我吧他的登陆时间保存的数据库,他离开的时候(直接关闭网页或者断电了)获得它离开的时间保存到数据库!我不知道该怎么写,就到网上看了下,科室捣鼓了半天没搞明白!到底是怎么监控,代码写了一点点不知道对不对,而且是在写不下去了,希望各位大侠帮帮忙给修改或者完善下代码!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using
using
using

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);
                }
            }
        }
系统为B/s结构,谢谢大家了!
3 回复
#2
songgaotong2010-12-01 12:40
在线等!
#3
songgaotong2010-12-01 13:24
好吧此问题有点愚蠢,我换了个方法解决了,不过我还是希望有人能把这个监控补充完整!大家也都可以学习 谢谢!

#4
冰镇柠檬汁儿2010-12-01 20:40
bs的程序是放到服务器上的,而客户端是否关机是不可能通知服务器端的,就像我的机器关机了不会告诉你一样。
所以没有直接的办法做到,至少我不会
不过我有一个间接的办法,在数据库中定义一个值,比如2,然后每个1分钟将这个值统一递减1,而用户登录后,每个半分钟将这个值改成2,直到用户的这个值小于等于0的时候,就认为用户已经退出系统,这样即使用户突然断电关机,你这边也能处理了。不过这个方法很消耗系统的性能
1