Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
Creating a Windows Service in J#
Messages   Related Types
This message was discovered on microsoft.public.dotnet.vjsharp.


Charles Brauer
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

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
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.

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.ComponentModel.RunInstallerAttribute
System.Diagnostics.EventLog
System.Object
System.ServiceProcess.ServiceAccount
System.ServiceProcess.ServiceBase
System.ServiceProcess.ServiceInstaller
System.ServiceProcess.ServiceProcessInstaller
System.ServiceProcess.ServiceStartMode
System.Threading.Timer
System.Timers.ElapsedEventArgs
System.Timers.ElapsedEventHandler
System.Timers.Timer
System.Windows.Forms.Timer




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734