用.NET创建Windows服务__教程 |
|
日期:2007-5-20 0:36:51 人气:98 [大 中 小] |
|
|
|
/// protected override void OnStart(string[] args) { this.timer1.Enabled = true; this.LogMessage("Service Started"); } /// /// Stop this service. /// 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(); } } } }
安装Windows服务 Windows服务不同于普通Windows应用程序。不可能简简单单地通过运行一个EXE就启动Windows服务了。安装一个Windows服务应该通过使用.NET Framework提供的InstallUtil.exe来完成,或者通过诸如一个Microsoft Installer (MSI)这样的文件部署项目完成。 |
|
出处:本站原创 作者:佚名 |
|
|