进程服务编写,与启动停止控制

进程编写很简单,可以创建,一个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);
        }


        
    }

}


posted on 2005-10-23 10:29  edobnet  阅读(3058)  评论(3编辑  收藏  举报