windows服务的构成 在你类后面所包含的代码里,你会注意到你所创建的windows服务扩充了system.serviceprocess.service类。所有以.net方式建立的windows服务必须扩充这个类。它会要求你的服务重载下面的方法,visual studio默认时包括了这些方法。 • dispose – 清除任何受控和不受控资源(managed and unmanaged resources) • onstart – 控制服务启动 • onstop – 控制服务停止 数据库表脚本样例 在这个例子中使用的数据库表是使用下面的t-sql脚本创建的。我选择sql server数据库。你可以很容易修改这个例子让它在access或任何你所选择的别的数据库下运行。 create table [dbo].[myservicelog] ( [in_logid] [int] identity (1, 1) not null, [vc_status] [nvarchar] (40) collate sql_latin1_general_cp1_ci_as not null, [dt_created] [datetime] not null ) on [primary]
windows服务样例 下面就是我命名为myservice的windows服务的所有源代码。大多数源代码是由visual studio自动生成的。 using system; using system.collections; using system.componentmodel; using system.data; using system.data.sqlclient; using system.diagnostics; using system.serviceprocess; namespace codeguru.mywindowsservice { public class myservice : system.serviceprocess.servicebase { private system.timers.timer timer1; /// <remarks> /// required designer variable. /// </remarks> private system.componentmodel.container components = null; public myservice() { // this call is required by the windows.forms // component designer. initializecomponent(); } // the main entry point for the process static void main() { system.serviceprocess.servicebase[] servicestorun;
servicestorun = new system.serviceprocess.servicebase[] { new myservice() }; system.serviceprocess.servicebase.run(servicestorun); } /// <summary> /// required method for designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void initializecomponent() { this.timer1 = new system.timers.timer(); ((system.componentmodel.isupportinitialize) (this.timer1)).begininit(); // // timer1 // this.timer1.interval = 30000; this.timer1.elapsed += new system.timers.elapsedeventhandler(this.timer1_elapsed); // // myservice // this.servicename = "my sample service"; ((system.componentmodel.isupportinitialize) (this.timer1)).endinit(); } /// <summary> /// clean up any resources being used. /// </summary> protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } /// <summary> /// set things in motion so your service can do its work. /// </summary> protected override void onstart(string[] args) { this.timer1.enabled = true; this.logmessage("service started"); }
/// <summary> /// stop this service. /// </summary> protected override void onstop() { this.timer1.enabled = false; this.logmessage("service stopped"); } /* * respond to the elapsed event of the timer control */ private void timer1_elapsed(object sender, system.timers.elapsedeventargs e) { this.logmessage("service running"); } /* * log specified message to database */ private void logmessage(string message) { sqlconnection connection = null; sqlcommand command = null; try { connection = new sqlconnection( "server=localhost;database=sampledatabase;integrated security=false;user id=sa;password=;"); command = new sqlcommand( "insert into myservicelog (vc_status, dt_created) values (" + message + ",getdate())", connection); connection.open(); int numrows = command.executenonquery(); } catch( exception ex ) { system.diagnostics.debug.writeline(ex.message); } finally { command.dispose(); connection.dispose(); } } } }