惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
博客园 - 聂微东
罗磊的独立博客
W
WeLiveSecurity
博客园_首页
Scott Helme
Scott Helme
V
Visual Studio Blog
T
The Exploit Database - CXSecurity.com
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
L
Lohrmann on Cybersecurity
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
About on SuperTechFans
F
Full Disclosure
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
博客园 - Franky
C
CXSECURITY Database RSS Feed - CXSecurity.com
F
Fortinet All Blogs
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
雷峰网
雷峰网
博客园 - 【当耐特】
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
T
Tor Project blog
V
V2EX
爱范儿
爱范儿
C
Check Point Blog
T
Threatpost
Project Zero
Project Zero
量子位
V
Vulnerabilities – Threatpost
Know Your Adversary
Know Your Adversary
I
Intezer
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - edobnet

phonegap 开发初探 .net C# ADC接口中DES加密算法 阿里软件接口开发基础(淘宝网) C# SQL 2005自动备份文件删除 短信猫与中国移动CMPP2。0收发短信实例 通过OSQL命令执行SQL SERVER批SQL 针对sql 2005优化的高性能分页存储过程 .net framework 2.0以上修改config配制更容易 - edobnet LinQ操作汇总(From CSharpSamples) 远程注册表读取,与多线程池的应用. 使用JAVASCRIPT控制,IFAME的内容,从而使用投票点击等功能 ADSL自动断拨号类 类拟EXCEL表格锁定的功能的实现! - edobnet - 博客园 线程,线程数控制! sql Server 2000 分区视图的运用 利用.Net 线程池提高应用程序性能. google 中国编程大赛测试题,及自己的解答 触发器在增量同步数据的运用. js异步XMLhttpPost,并带有,等待显示,防正,XMLhttpPost请求时间过来,而使浏览器死掉! - edobnet - 博客园
进程服务编写,与启动停止控制
edobnet · 2005-10-23 · via 博客园 - edobnet

进程编写很简单,可以创建,一个WINDOWS服务的项目就行,不过创建以后没有相关安装服务的配制,可以添加一个安装配制文件,
添加系统DLL引用,System.Configuration.Install
代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

namespace TransferService
{
    
/// <summary>
    
/// ProjectInstaller 的摘要说明。
    
/// </summary>

    [RunInstaller(true)]
    
public class ProjectInstaller : System.Configuration.Install.Installer
    
{
        
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
        
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public ProjectInstaller()
        
{
            
// 该调用是设计器所必需的。
            InitializeComponent();

            
// TODO: 在 InitializeComponent 调用后添加任何初始化
        }


        
/// <summary> 
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if(components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }



        
组件设计器生成的代码
    }

}

主要服务运算代码可以在,  Service1.cs的static void Main()或构造函数里写,
如果要编写程序控制服务,.Net Framework有一个, System.ServiceProcess.ServiceController的类,可以控制服务器,起动与暂停,停止等,默认没有暂停功能,如果需要,在服务代码里,需要,OVer相应的代码,默认Onstart与OnStop已经Override,
System.ServiceProcess.ServiceController控制起来很简单,设置属性:MachineName(服务器名)与ServiceName(进程名)就可以获得进程的状态了和控制了,
这个的进程名是对应Install.cs里的, this.serviceInstaller1.ServiceName = "YJC.Transfer.Service";这个属性
不过要次控制需要使用,Refresh();方法进行刷新,
相应我的代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.ServiceProcess;
using KitDescriptor;
namespace ServiceManager
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    [Kit("ServiceMange", DisplayName = "后台服务管理", PictureName = "WebCargoForm.gif", Author = "WangYJ"
         Description 
= "服务管理", DisplayOrder = 1)]
    
public class AppMainForm : FormSolution.Library.UI.EditForm
    
{
        
private System.Windows.Forms.ComboBox cb_Service;
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private FormSolution.Library.CustomControl.XPButton.XPButton btn_start;
        
private System.ServiceProcess.ServiceController fSericeControl;
        
private System.Windows.Forms.TextBox txt_MachineName;
        
private FormSolution.Library.CustomControl.XPButton.XPButton btn_Parse;
        
private FormSolution.Library.CustomControl.XPButton.XPButton btn_stop;
        
private FormSolution.Library.CustomControl.XPButton.XPButton btn_install;
        
private FormSolution.Library.CustomControl.XPButton.XPButton btn_uninstall;
        
private FormSolution.Library.CustomControl.XPButton.XPButton btn_refresh;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.GroupBox groupBox1;

        
private DataSet ds = null;
        
public AppMainForm()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();
            ds 
= new DataSet();
        
//    cb_Service.DataSource = ServiceManager.GetObject.Tables[0];
            string path = Application.StartupPath+"\\service.config";
            ds.ReadXml(path,XmlReadMode.ReadSchema);
            
            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
            foreach(DataRow row in ds.Tables[0].Rows)
            
{
                cb_Service.Items.Add(row[
"DisplayName"]);
            }

            
            
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
Windows 窗体设计器生成的代码

        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new AppMainForm());
        }


        
private void cb_Service_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            fSericeControl.MachineName 
= txt_MachineName.Text;
            fSericeControl.ServiceName 
= getRow()["ServiceName"].ToString();
            btn_refresh.Enabled 
= true;
            CheckStatus();
        }

        
public void CheckStatus()
        
{
            System.Threading.Thread.Sleep(
100);
            btn_start.Enabled 
= false;
            btn_stop.Enabled 
= false;
            btn_Parse.Enabled 
= false;
            btn_install.Enabled 
= false;
            btn_uninstall.Enabled 
= false;
            
try
            
{
                
if(fSericeControl.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                
{
                    btn_start.Enabled 
= true;
                    btn_uninstall.Enabled 
= true;
                }

                
if(fSericeControl.CanPauseAndContinue)
                
{
                    btn_Parse.Enabled 
= true;
                }

                
if(fSericeControl.CanStop)
                
{
                    btn_stop.Enabled 
= true;
                }

            }

            
catch
            
{
                btn_install.Enabled 
= true;

            }


        }


        
private void btn_install_Click(object sender, System.EventArgs e)
        
{
            
try
            
{
                CommandControls.ExcuteCmd(getRow()[
"InstallPath"].ToString());
                System.Threading.Thread.Sleep(
1000);
                ShowMess(
"安装成功!");
            }

            
catch(Exception ex)
            
{
                ShowMess(ex.ToString());
            }

            
            CheckStatus();
        }


        
private void btn_Parse_Click(object sender, System.EventArgs e)
        
{
            
if(fSericeControl.Status == System.ServiceProcess.ServiceControllerStatus.Running)
            
{
                fSericeControl.Pause();
                btn_Parse.Text 
= "继续";
            }

            
else
            
{
                fSericeControl.Continue();
                btn_Parse.Text 
= "暂停";
            }

            CheckStatus();
        }


        
private void btn_stop_Click(object sender, System.EventArgs e)
        
{
            fSericeControl.Stop();
            fSericeControl.Refresh();
            CheckStatus();
        }


        
private void btn_start_Click(object sender, System.EventArgs e)
        
{
        
            fSericeControl.Start();
            fSericeControl.Refresh();
            CheckStatus();
                
        }


        
private void btn_refresh_Click(object sender, System.EventArgs e)
        
{
            fSericeControl.Refresh();
            CheckStatus();
        }


        
private void btn_uninstall_Click(object sender, System.EventArgs e)
        
{
            
try
            
{
                CommandControls.ExcuteCmd(getRow()[
"UninstallPath"].ToString());
                System.Threading.Thread.Sleep(
1000);
                ShowMess(
"卸载成功!");
            }

            
catch(Exception ex)
            
{
                ShowMess(ex.ToString());
            }

            
            CheckStatus();
        }

        
public DataRow getRow()
        
{
            
if(cb_Service.Text != "")
                
return ds.Tables[0].Rows[cb_Service.SelectedIndex];
            
else
                
return null;
        }

        
public void ShowMess(string msg)
        
{
            MessageBox.Show(msg);
        }


        
    }

}