退出的时候thread.join()了没有啊

解决方案 »

  1.   

    线程问题,开始服务后,线程启动,但是现在作为前台线程,前台线程和主程序是同一级别,所以当停止服务了,线程没有结束,所以程序没有结束.解决办法,把它做为后台线程,加一句:
    thread.isback....//我现在没有编译器,这个词记不清楚了随便说句,将前台线程放到死循环里面是非常卡的事情,呵呵.
      

  2.   

    //启动服务代码
    public override  void OnStratr()
    {
    thread = new Thread(new ThreadStart(this.Listeners));
    //
    //加这个地方
    thread.Start();
    }
      

  3.   

    这个问题我最近也遇到了,是你在推出程序的时候没有关闭线程的问题。可以在主线程退出的时候手动调用thread.abort
      

  4.   

    windows服务不能由自己编写代码来结束,必须由外部的windows服务监视器来结束.关于远程机子的windows监控问题可以采用System.Management命名空间下的ManagementClass和ManagementObject类来完成.关键代码如下:
    class partial Win32ServiceManage
        {
            private ManagementClass severManager;
            private string strPath;
            private List<Win32Service> _ServiceList;
            private bool _IsLocal;
            private string _HostName;
            private string _UserName;
            private string _Password;        public bool IsLocal
            {
                get { return _IsLocal; }
            }        public string HostName
            {
                get { return _HostName; }
            }        public string UserName
            {
                get { return _UserName; }
            }        public string Password
            {
                get { return _Password; }
            }        public Win32ServiceManage() 
            {
                _IsLocal = true;
                _ServiceList = new List<Win32Service>();
            }        public Win32ServiceManage(string host, string userName, string password)
            {
                _IsLocal = false;
                _ServiceList = new List<Win32Service>();
                strPath = "\\\\" + host + "\\root\\cimv2:Win32_Service";
                severManager = new ManagementClass(strPath);
                if (userName != string.Empty && userName != null 
                    && password!=string.Empty && password!=null
                    && host!=string.Empty && host!=null)
                {
                    _HostName = host;
                    _Password = password;
                    _UserName = userName;
                    ConnectionOptions option = new ConnectionOptions();
                    option.Username = _UserName;
                    option.Password = _Password;               
                    ManagementScope mscope = new ManagementScope(strPath, option);
                    severManager.Scope = mscope;
                }
            }        public static bool RemoteConnectValidate(string host, string userName, string password)
            {
                string path = "\\\\" + host + "\\root\\cimv2";
                ConnectionOptions option = new ConnectionOptions();
                option.Password = password;
                option.Username = userName;
                ManagementScope mscope = new ManagementScope(path, option);
                try
                {
                    mscope.Connect();
                }
                catch{}           
                return mscope.IsConnected;
            }               public List<Win32Service> GetServiceList()
            {
                _ServiceList.Clear();
                if(_IsLocal==false)
                {                
                    foreach (ManagementObject service in severManager.GetInstances())
                    {
                        _ServiceList.Add(new Win32Service(service));
                    }               
                }
                else
                {
                   
                    try
                    {
                        SelectQuery query = new SelectQuery("Select * From Win32_Service");
                        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                        foreach(ManagementObject service in searcher.Get())
                        {                        _ServiceList.Add(new Win32Service(service));
                        }
                    }
                    catch{}                
                }
                return _ServiceList;
            }        public List<Win32Service> GetServiceList(string serviceName)
            {
                return GetServiceList(new string[] { serviceName });
            }        public List<Win32Service> GetServiceList(string[] servicesName)
            {
                List<Win32Service> servicess = new List<Win32Service>();   
                if(_IsLocal==false)
                {
                    foreach (string name in servicesName)
                    {
                        ManagementObject service = severManager.CreateInstance();
                        service.Path = new ManagementPath(".Name=\"" + name + "\"");                   
                        servicess.Add(new Win32Service(service));
                    }
                }
                else
                {
                    foreach(Win32Service winService in _ServiceList)
                    {
                        foreach(string name in servicesName)
                        {
                            if(winService.Name==name)
                            {
                                servicess.Add(winService);
                            }
                        }
                    }
                }       
                return servicess;
            }        }
      

  5.   

    partial class  Win32ServiceManage
    {        public string StartService(string service)
            {
                string message = string.Empty;
                try
                {
                    if(_IsLocal==false)
                    {
                        ManagementObject myservice = severManager.CreateInstance();
                        myservice.Path = new ManagementPath(strPath + ".Name=\"" + service + "\"");
                        Win32Service winService = new Win32Service(myservice);
                        winService.StartService();
                    }
                    else
                    {
                        List<Win32Service> servicelist = GetServiceList(service);
                        servicelist[0].StartService();
                    }               
                }
                catch (ManagementException e)
                {
                    message = e.Message;
                }
                return message;
            }        public string PauseService(string service)
            {
                string message = string.Empty;
                try
                {
                    if(_IsLocal==false)
                    {
                        ManagementObject myservice = severManager.CreateInstance();
                        myservice.Path = new ManagementPath(strPath + ".Name=\"" + service + "\"");
                        Win32Service winService = new Win32Service(myservice);
                        winService.PauseService();
                    }
                    else
                    {
                        List<Win32Service> servicelist = GetServiceList(service);
                        servicelist[0].PauseService();
                    }      
                }
                catch (ManagementException e)
                {
                    message = e.Message;
                }
                return message;
            }        public string ResumeServie(string service)
            {
                string message = string.Empty;
                try
                {
                    if(_IsLocal==false)
                    {
                        ManagementObject myservice = severManager.CreateInstance();
                        myservice.Path = new ManagementPath(strPath + ".Name=\"" + service + "\"");
                        Win32Service winService = new Win32Service(myservice);
                        winService.ResumeServie();
                    }
                    else
                    {
                        List<Win32Service> servicelist = GetServiceList(service);
                        servicelist[0].ResumeServie();
                    }      
                }
                catch (ManagementException e)
                {
                    message = e.Message;
                }
                return message;
            }        public string StopService(string service)
            {
                string message = string.Empty;
                try
                {
                    if(_IsLocal==false)
                    {
                        ManagementObject myservice = severManager.CreateInstance();
                        myservice.Path = new ManagementPath(strPath + ".Name=\"" + service + "\"");
                        Win32Service winService = new Win32Service(myservice);
                        winService.StopService();
                    }
                    else
                    {
                        List<Win32Service> servicelist = GetServiceList(service);
                        servicelist[0].StopService();
                    }     
                }
                catch (ManagementException e)
                {
                    message = e.Message;
                }
                return message;
            }
    }
    1.该类实现了监控本地和远程Windwos服务两种功能.
    其中构造函数语句:
     strPath = "\\\\" + host + "\\root\\cimv2:Win32_Service";用于连接远程服务的路径.
    2.RemoteConnectValidate方法用于检测是否能连到远程主机.
    3.当调用了构造函数之后,就可以采用ManagementClass类的CreateInstance方法创建ManagementObject对象,然后为该对象的Path属性赋值以便定向到某个windows服务.
    4.当调用了构造函数之后,就可以采用ManagementClass类的GetInstances方法得到所有的ManagementObject对象集合,因为在构造函数中已经指明了windows服务路径,因此GetInstances方法是得到某台机子上所有的windows服务
    5.如果要停止某个windows服务,只需调用StartService方法限可.