This message was discovered on microsoft.public.dotnet.vjsharp.
| Charles Brauer |
| GOOD ANSWER |
Hello all,
Can anyone point me to sample code that can be run as a Windows Service. I would like to run this code in the background as a service that is automatically re-started when the server is re-restarted.
I believe that I need to inherit from System.Service.ServiceBase, but I'm not sure of how to do this in J#.
Any help will be greatly appreciated.
Charles
|
|
|
| |
|
|
| |
| |
| Robert LaCasse |
| GOOD ANSWER |
Hello - Here is a quick sample I posted a while back for this. It's a port of a C# Windows Service Sample. Note that I changed this to prompt for a user account. This should work as skeleton code and to help you get started, but you'll want to have a look at the documentation for this API. You can also configure your sevice using mmc (Microsoft Management Console). Set it to "automatic" to have it start when the system boots.
Regards, -Bob
... This is the result of taking the C# QuickStart sample and re-coding it to VJ# syntax, etc. I've left some of the original C# commented in the source. See the MSDN docs and the QuickStart sample for info. on the classes used and you should be okay. Also, need the installutil.exe tool found in %SystemRoot%\Microsoft.NET\Framework\vx.x.xxxx
Regards, -Bob ---------------------------------------------------------------------------- -------- import System.Collections.*; import System.Configuration.Install.*; import System.ServiceProcess.*; import System.ComponentModel.*;
//[RunInstallerAttribute(true)] /** @attribute RunInstallerAttribute(true) */ public class MyInstaller extends Installer { private ServiceInstaller serviceInstaller; private ServiceProcessInstaller processInstaller;
public MyInstaller() { processInstaller = new ServiceProcessInstaller(); serviceInstaller = new ServiceInstaller();
// Service will run under system account //processInstaller.Account = ServiceAccount.LocalSystem; processInstaller.set_Account(ServiceAccount.User);
// Service will have Start Type of Manual //serviceInstaller.StartType = ServiceStartMode.Manual; serviceInstaller.set_StartType(ServiceStartMode.Manual);
//serviceInstaller.ServiceName = "Hello-World Service"; serviceInstaller.set_ServiceName("Hello-World Service");
//Installers.Add(serviceInstaller); get_Installers().Add(serviceInstaller); //Installers.Add(processInstaller); get_Installers().Add(processInstaller); } } ---------------------------------------------------------------------------- --------------------------------- import System.ServiceProcess.*; import System.Diagnostics.*; import System.Timers.*;
public class MyService extends ServiceBase { protected Timer timer;
public static void main() { ServiceBase.Run(new MyService()); }
public MyService() { //CanPauseAndContinue = true; this.set_CanPauseAndContinue(true); //ServiceName = "Hello-World Service"; this.set_ServiceName("Hello-World Service");
timer = new Timer(); //timer.Interval = 1000; timer.set_Interval(1000); //timer.Elapsed += new ElapsedEventHandler(OnTimer); timer.add_Elapsed(new ElapsedEventHandler(OnTimer)); }
protected void OnStart(String[] args) { //EventLog.WriteEntry("Hello-World Service started"); get_EventLog().WriteEntry("Hello-World Service started"); //timer.Enabled = true; timer.set_Enabled(true); }
protected void OnStop() { //EventLog.WriteEntry("Hello-World Service stopped"); get_EventLog().WriteEntry("Hello-World Service stopped"); //timer.Enabled = false; timer.set_Enabled(false); }
protected void OnPause() { //EventLog.WriteEntry("Hello-World Service paused"); get_EventLog().WriteEntry("Hello-World Service paused"); //timer.Enabled = false; timer.set_Enabled(false); }
protected void OnContinue() { //EventLog.WriteEntry("Hello-World Service continued"); get_EventLog().WriteEntry("Hello-World Service continued"); //timer.Enabled = true; timer.set_Enabled(true); }
protected void OnTimer(System.Object source, ElapsedEventArgs e) { //EventLog.WriteEntry("Hello World!"); get_EventLog().WriteEntry("Hello World!"); } }
Bob LaCasse Microsoft Beta Support - Visual J#.NET
This posting is provided “AS IS” with no warranties, and confers no rights.
Bob LaCasse Microsoft Beta Support - Visual J#.NET
This posting is provided “AS IS” with no warranties, and confers no rights.
|
|
|
| |
|
|
| |
|
|
|
|
|