Computing.Net > Forums > Programming > VB.NET service

VB.NET service

Reply to Message Icon

Original Message
Name: ashtank
Date: April 25, 2005 at 04:33:51 Pacific
Subject: VB.NET service
OS: windows 2000
CPU/Ram: 2.2 Mhz 256 MB
Comment:

I am assigned to develop a small application which is to be developed on VB.NET but with a option that the application could be run as a service. Since i m new to .NET programming, can someone gimme a clue where to start or some tutorial link?


Report Offensive Message For Removal

Response Number 1
Name: Chi Happens
Date: April 25, 2005 at 08:11:44 Pacific
Subject: VB.NET service
Reply: (edit)

all you need to do is choose Windows Service from the list of projects you can make and .net will do most of the framework for you.

Then you need to add an installer to the project (which allows you to install the exe as a service onto the machine)

if you need the service to perform based on time, you need to add a System.Timers.Timer (which is thread-based) because the System.Windows.Forms.Timer will not work.

There are several events you should account for:

OnStart()
OnStop()
OnPause()
OnContinue()


I have made about 25 services so far using .NET. I program in C#, but you should be able to translate them to VB.net (although I don't know why anyone would want to program in vb.net when c# is available...anyhow i digress) that I would be willing to share with you.

Chi

They mostly come at night...mostly


Report Offensive Follow Up For Removal

Response Number 2
Name: ashtank
Date: April 26, 2005 at 03:00:29 Pacific
Subject: VB.NET service
Reply: (edit)

Thanx Chi,

But will u pls tell me how to provide an option to the windows application to run as a service? I have seen few applications which have options that they can be run as service also. Second, i will be obliged if u cud share some mediocre project in terms of difficulty to start with my project.

many thanks

Ashtank


Report Offensive Follow Up For Removal

Response Number 3
Name: Chi Happens
Date: April 26, 2005 at 07:09:17 Pacific
Subject: VB.NET service
Reply: (edit)

It very very simple.

1) Create a windows service using the windows service wizard.
2) Create your classes that you want to share between the service and the windows app.
3) Add a form to use the user-interface when the program is not run as a service.
4) Modify the Main to accept command-line switches.
5) Make the command line switch choose to create the service or create the form.

like this:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

namespace WindowsService1
{
    public class Service1 : System.ServiceProcess.ServiceBase
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Service1()
        {
            // This call is required by the Windows.Forms Component Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitComponent call
        }

        // The main entry point for the process
        static void Main(string[] args)
        {
            if(args.Length > 0)
            {
                Form1 form1 = new Form1();
                form1.ShowDialog();
            }
            else
            {
                System.ServiceProcess.ServiceBase[] ServicesToRun;
    
                // More than one user Service may run within the same process. To add
                // another service to this process, change the following line to
                // create a second service object. For example,
                //
                //   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
                //
                ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

                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()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = "Service1";
        }

        /// <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)
        {
            // TODO: Add code here to start your service.
        }
 
        /// <summary>
        /// Stop this service.
        /// </summary>
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
        }
    }
}


Hope this helps,
Chi


They mostly come at night...mostly


Report Offensive Follow Up For Removal

Response Number 4
Name: ashtank
Date: April 27, 2005 at 23:26:29 Pacific
Subject: VB.NET service
Reply: (edit)

Chi, its of immense help. many thanx. But i have still one issue which i cuddn't understand. I have seen appls compiled as exes and with option to run them as service. When u chk the checkbox, they start running as service when u close the UI. when u again try to run exe, they pop up with msg that one shud stop the service to make any option changes. Can u pls tell me how this can be done?

Many tx

Ash


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: VB.NET service

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software