Working with Windows Service Using Visual Studio 2005
page 1 of 9
Published: 05 Jul 2007
In this article Abhishek describes how to implement, setup, install, and debug Windows Services with the help of snap-shots.
by Abhishek Kumar Singh
Average Rating: 
Views (Total / Last 10 Days): 52345/ 93


·         A windows service is a program which runs continuously in the background of an Operating System. It was formerly known as NT services. Window service internally uses the Service Control Manager (SCM) of the operating system. A service must be installed in a SCM database before it can be launched. SCM controls the entire lifetime of each installed service.

·         Implementing windows services has become the choice of the current era of business as it requires minimum human intervention after deployment and gives long running functionality. We also say window service application works as an Engine; once started it will keep running until the machine shutdowns or gets some abnormal error.

View Entire Article

User Comments

Title: Non-blocking timer   
Name: James Collett
Date: 2012-08-05 7:46:13 AM
You write: "Each thread will wait for the previous thread to finish by making a FIFO (first in first out) queue."

Perhaps I have misunderstood this, but I do not think each thread does wait for the previous one to finish.

I have inserted a sleep command just after the "firing EventAction" line is written to the file. I have made the sleep period 180 seconds, but the next thread nevertheless writes its line of text to the file after one minute, not waiting the extra 30 seconds.

I should like to ensure that if the current thread takes more than 60 seconds to complete its task, then the next thread is blocked from starting until the first thread finishes. How can I do this, please?
Title: question   
Name: Ranjini
Date: 2012-04-07 2:24:32 PM
After each and every updation in the vb project the system needs to be restarted ,am using sql server 2005 .i think some file may miss in the server while installing. now i cant reinstall , a lot of tables are added in the sql.any other solution for this???????
Title: Nice Article but one query   
Name: Fawwad
Date: 2012-03-13 1:52:14 AM
Nice article. I just had one question. what change would I have to make if I wanted the installer to ask the user for the service name that he want the service to be installed with.
Is that even possible?

Awaiting your reply
Title: Reply from Author-abhishek   
Name: Abhishek
Date: 2012-02-17 5:25:35 AM
Hi Guys, It is very nice to hear that my article is useful for you. Thanks for your comments.

Title: Yours worked - unlike most other tutorials   
Name: J. Mason
Date: 2012-01-31 9:46:33 PM
Was getting very frustrated not getting the timer to fire. Yours was the only example I found that the timer was set up and actually worked.

Debugging - most examples to attach the process, including yours, did not work. I had to add a line of code in OnStart System.Diagnostics.Debugger.Launch()
Title: Excellent Tutorial   
Name: H. Nguyen
Date: 2010-10-19 9:48:25 PM
Excellent! Concise, short, and complete (from programming, installing and debugging). Thank you.
Title: Very Good   
Name: Rohit
Date: 2010-10-14 2:28:25 AM

It is very good article and the main thing that is in this service the screen-shots have been given, so it is easy for new learners.


Title: Excellent Article   
Name: O.Shathish
Date: 2010-07-14 2:39:52 AM
Hi Abhishek,

It is really good article. I find easily the things what i have to do, in this article.

Title: problem installing   
Name: Daniel Rossell
Date: 2010-05-28 10:07:03 AM
I have a similar (or even the same) problem that has BMW. I can't check if it works on XP or win7 but I can't install in windows vista.

the message error is something like "There was no new mapping between account names and
Security Identifiers"

[I translated it with google so it's not the exact error message. the original message (in spanish) is "no se efectuó ninguna asignación entre los nombres de cuenta y los identificadores de seguridad"]

I hope somebody can help us to find the error.
Title: Mr   
Name: Ashok
Date: 2010-05-12 8:27:47 PM
Nice article. Very easy to follow. Thanks. Ashok
Title: Failed to install on Windows 7   
Name: BMW
Date: 2010-04-29 3:46:51 PM
I created the windows service and successfully installed on windows XP. However, it is failed on the Windows 7. Is it any idea for me to solve the problem?
Title: Great article   
Name: hurling0
Date: 2010-04-28 11:31:42 PM
A very understandable article for Windows Service. Thanks~
Title: Working with Windows Service Using Visual Studio 2005   
Name: Sushil
Date: 2010-04-22 9:51:31 AM
Thanks man good article
Title: Thanks   
Name: Rameez Syed(Jeevantechnologies)
Date: 2010-04-09 10:47:18 AM
Title: Facing Problem in windows service   
Name: prateek saluja
Date: 2010-04-03 4:04:52 PM
i did all thing step by step as there shown.but i faced the the above diagram there is 2 file one i .vshost.exe
and another is .exe but when i install a setup i did not get this .exe file into Attach to process Dialog box.for ex if my project name is TestingWindowsservice so only TestingWindowsservice.vshost.exe file is there im not getting any TestingWindowsservice.exe file which i need to attach. ihave beb tried thi thing into google but still the problem is plz help me to shortout this problem.thanx
Title: Thanks alot   
Name: Dadang Setiawan
Date: 2010-03-26 3:38:46 AM
Very great2 thanks abhisek for the article... very2 clear.. Im looking around for this kind of article..
Title: Feedback   
Name: Nada
Date: 2010-03-21 5:07:36 AM
excellent explanation
Title: Online installers   
Name: Semyon
Date: 2010-02-11 1:34:20 PM
Just to add some tip to this excellent piece :) If you don't want to create your own installer, you can use online services that let you build your installers, .msi or even .exe without writing a code or debugging it. One that comes to mind is at
Title: Feedback   
Name: .net Prog
Date: 2010-02-10 3:08:37 AM
Very Very helpful article Thanks a lot
Title: Nice Article   
Name: Muhammad Faiq Bakhtyar
Date: 2010-02-09 7:19:34 AM
Hi, nice and as simple as 1+1....great job
kindly post new articles as well.
Title: Feedback   
Name: Vikram selvaraj(Manaparai)
Date: 2010-02-02 7:14:50 AM
Hi, I am getting clear idea about windows services,thanks for posting this article,please post more articles in future.
Title: Abhishek's reply to abhi   
Name: Author
Date: 2010-01-17 3:01:31 PM
hi abhi.. you need to be sure that you service is doing some sort of job in start as well to avoid this error.
Title: message   
Name: abhi
Date: 2010-01-11 5:58:21 AM
hii.. i am getting message "Some services stop automatically if they have no work to do,"i try to start the my service.. wat to do?
Title: Excellent the best!   
Name: smayshar
Date: 2009-11-05 7:43:02 AM
Thank you
my first service with in minutes running!
Title: Best article   
Name: Sandeep
Date: 2009-11-03 6:18:52 PM
Hi Abhishek,
Thankyou for writing this article.
Very well explained step by step with images.
my service is up and running after going through ur article.
Thanks again and keep the good work going....
Title: From Sreeachanta   
Name: Sree Achanta
Date: 2009-10-27 2:25:44 AM
Good Article
Title: Help   
Name: Kanchan
Date: 2009-10-23 12:25:59 PM

I have a problem,I started my service. But i cannot debug. I have also attached to process. but it doesn't hit my event.
Title: for website creation   
Name: abhishek
Date: 2009-10-01 5:33:18 PM
hi neha.. thanks for appreciating me. By the way.. to create website in VS2005:
File>> New >> WebSite
For the location you can select FileSystem for the learning purpose >> Specify the website folder
Choose the language C# or VB >> OK
Here a blank website project will be opened. now place your required controls (textbox, buttions , grids etc.) from the toolbox.
Run the website (project) by pressing F5
Here you can see your web page. That's it.
You can google the tutorials for more info. Enjoy!!!
Title: For Help   
Name: Neha Sharma
Date: 2009-10-01 2:21:11 AM
I m in a problem that how to create a website. But M trying my beat.But now I want help from u coz i have seen ur profile.I think u r master in this field.
So can u help me??????I want to know-
1.WHat should i choose NEw Web site or NEw project?(I am first time on Visual studio 2005).
2.What are the steps to create any site?????????
Title: From abhishek   
Name: abhishek
Date: 2009-08-01 1:34:18 PM
Thanks Dhirendra for your trick.
Title: Good artical   
Name: Dhirendra
Date: 2009-07-07 6:36:22 AM
Just a Trick for question 2:Can we debug the OnStart method of windows service?

yes we can just Add a Thread.sleep(40000); in onStart event.
place a break point. start the service attch the service with debugger.
Title: Very Good Article   
Name: Suman
Date: 2009-06-15 5:39:22 AM
Thanks for sharing such nice article. Any one can understand by going through this. Good work. Keep doing so.
Title: Great Article Thanks a lot   
Name: Sai krishna
Date: 2009-04-29 6:37:27 AM
Hi Thanks a lot for your article which help me a lot in knowing the windowsservices and it absolutely easy to understand the way which u say.
Title: Thanks   
Name: Abhishek Kumar Singh (Author)
Date: 2009-04-28 9:19:20 AM
Thanks Kamlesh.. I am happy to know you got your solution in through this article... Cheers!
Title: Great Article!!!   
Name: Kamesh Telikicherla
Date: 2009-04-25 3:53:46 AM
First of all thanks abhishek for such a nice article..!
Actually I am looking for a solution to the timer problem.
It fires only for certain period of time, then it stops even though the service is running.

These two things mentioned in the article's comments really worked out for me..!


protected const iTIME_INTERVAL As Integer = 60000
protected System.Threading.Timer oTimer;
Title: Good but see this ..   
Name: Javs
Date: 2009-04-07 8:54:18 PM
Kinda good ...
you might like to see this too ..

Also, did you notice that the start paragraph of your article matches that of the msdn article ....
Title: Thanks   
Name: Jitender
Date: 2009-03-20 5:59:51 AM
It is really explained very well. I had followed the steps mentioned. And now i m going to test it

Thanks in advance
Title: Thank You   
Name: Rich
Date: 2009-02-23 4:31:10 PM
Your article is very clearly written. I got my service up and running the first time!.
Thanks again for your efforts!
Title: Thanks   
Name: Ramesh
Date: 2009-02-15 11:59:46 PM
Thank you very much.
Article is too good. I just implemented this concept in my project now.
Title: Excellent6 Article   
Name: Avinash Joshi
Date: 2009-01-29 11:28:57 AM
this is Very simple and Excellent article
Title: service fire event once   
Name: Yves Lechasseur
Date: 2009-01-27 12:27:50 PM
the right code should be:

Protected oTimer As System.Threading.Timer
Protected Const iTIME_INTERVAL As Integer = 60000

Protected Overrides Sub OnStart(ByVal args() As String)
Thats will correct the mistake in the code and the service keep running. For database conection to an SQL server on the Internet, use this type of connection string:

Dim connectionString As String = "Data Source=,9999;Password=xxxxx;User ID=yyyyy;Initial Catalog=your database"
9999 is the MSSQL ports

hope this will help

Great article by the way..

Title: Excellent   
Name: R Gruber
Date: 2009-01-26 2:38:02 PM
Thank you for a clear, functional article.
This has saved me alot of time.
Title: Great Dude Keep Going   
Name: Vivek
Date: 2009-01-24 8:22:04 AM
Hi Dude,
I Successfully created my first windows Service,thanks a lot.

Keep Going and Keep Posting..


Title: Excellent Article, Please solve my issue   
Name: Jay Mehta
Date: 2009-01-20 3:04:06 AM
Hi Abhishek i like your article a lot.
My issue is regarding to windows service, i've implemented windows service. in that my EventAction is doing 2 task
1) getting list of files from internet (Database)
2) downloading files from remote location

now issue is once service start up it works fine but it is not calling back EventAction again after interval time.
i've kept inverval time of 4 mins (240000). i've also close the DB connection and disposed all objects.

please help me.

Thank you.
Title: Awesome article   
Name: MrEd
Date: 2009-01-10 1:59:42 PM
Thanks for the article - really great. Exactly what I was looking for and worked the very first time - nice detail.
Title: Good one   
Name: Hunterspot
Date: 2008-12-18 1:57:24 PM
Really good one. Easy to understand and implement. Thank you.
Title: Thx for the article and One more thing...   
Name: Vaibhav Uchil
Date: 2008-11-17 6:34:05 AM
Abhi, thx a lot for the article. Very well written, in a lucid language. Got started with my first windows services with it. well done.

Only one thing, u have demostrated the debugging process through "Set Up" Project and Installing the service. Any code change in the service needs complete iteration of the installation steps.
I happened to bump into an msdn article, where i read this..
you can directly install and uninstall the debug version of your build from your project folder using InstallUtil command.
To Install:
InstallUtil YourWindowsService.exe

To Uninstall:
InstallUtil /u YourWindowsService.exe

It works and hence saves the debug time.
You might need to add the .net framework path to the PATH environment variable though...
Title: window service to start by webservice   
Name: Abhishek (author)
Date: 2008-10-25 5:09:30 PM
Srinivas, i think you may need to be sure that you are the server PROCESS ID correctly before trying to restart the service. one more thing i want to know whether your webservice and windows service are on same machine?
Title: problem   
Name: Srinivas
Date: 2008-10-22 5:57:24 AM
1) i have created and windows application A1 and installed on my system. i want the application to be started when i log into the system.
2) i have created webservice B1 to start the application A1 when the system starts. and i kept the code in such a way that is checks for A1 application every minute.
3) if the A1 application is not running it will automatically starts A1 application.

My problem is
1) First time when i restarted the system A1 is getting executed.when i closed the A1 application then the b1 webservice should start the A1 application but its not executing.
2) when my A1 application is running and when i am logging into database then its giving a message odbc drivers are not found... But i have odbc drivers in my system.
with out using the process of B1 webservice if i am running the A1 application directly then i am getting connected to database.

please can any one tell why it is happening? what is the solution?
Title: Excellent Job   
Name: Doug Kirschman
Date: 2008-10-17 4:22:28 PM
Very well written and easy to follow. I even got the debugger to attach. Thanks for one of the few articles that actually works as it says.
Title: more detail   
Name: Abhishe (author)
Date: 2008-10-15 11:31:59 AM
archana can i know your requirement in some more detail?
Title: need more help   
Name: archana
Date: 2008-10-13 2:01:07 AM
i want to use print document in service, how can i do it?
Title: Nice   
Name: Warren
Date: 2008-10-09 7:47:58 AM
Nice work thanks
Title: Nice Article. But cannot debug   
Name: siddiq
Date: 2008-10-07 5:41:22 AM
My service was running fine. But i cannot debug. I have also attached to process. but it doesn't hit eventaction sub
Title: Find Visual Studio 2005 Remote Debugger   
Name: Norman
Date: 2008-10-06 5:00:27 AM
Have anyone an idea where i can find a Downloadlink for Visual Studio 2005 Remote Debugger.

I can't find it.
Title: About Time   
Name: Paul
Date: 2008-09-23 4:19:08 AM
Very refeshing to find an article that works - thank you!
Title: From author   
Name: Abhishek Kumar Singh(Author)
Date: 2008-09-20 1:08:28 PM
Hi friends, I am sorry as I was not in regular touch with aspalliance for last few months. I came back now. Thanks for your appreciation. Hope you found this article useful. Let me know for your queries and suggestions. Thanks.
Title: Another happy reader   
Name: lneely
Date: 2008-09-18 4:04:47 PM
Great article. Well written, straight to the point. The illustrations help a lot. I was able to write a working service in less than 15 minutes using this article. Keep up the good work. -LN
Title: Good article   
Name: Felix
Date: 2008-09-16 4:42:13 PM
Excellent article
Title: Attach to Process in Tools Menu, Not Debug   
Name: Geoff Kenny
Date: 2008-08-27 1:15:33 PM
I was unable to find 'Attach to Process' in the Debug menu, but I did find it in the Tools menu.

Just an FYI.
Title: Nice Article   
Name: Sharath
Date: 2008-07-30 11:37:07 PM
Hi Abhi,

Thanks. I learn more about windows service with this article.
Title: How to implement for database communication   
Name: Richard
Date: 2008-07-08 10:54:55 AM
I am writing an small membership application. The program will probably run an a single machine but I need to way to allow any user that logs on to access the underlying database. I have heard that I could implement this feature through a windows service and use remoting. Could anyone give me a head start on this issue? BTW the program wil be distributed via CD-ROM so the installation needs to be as simple as possible.
Title: service fire event once   
Name: henry
Date: 2008-07-03 2:13:09 PM
window service project fire event once. even when i have made
Public oTimer As System.Threading.Timer
Const iTIME_INTERVAL As Integer = 60000
public variable
Title: Populating SQL table using windows service   
Name: abhishek
Date: 2008-06-19 3:00:12 AM
hi madhu,
did you find the solution?
Title: Populating SQL table using windows service   
Name: madhu
Date: 2008-06-05 12:50:39 PM
Hi Abhishek,

I am trying to populate SQL tables using windows service,this windows service will have reference to COM objects,data is pulled from Lotus Notes Database and update it to SQL table.This application is already developed and it worked fine until we stopped it few days back and when we restarted the application it is not populating the SQL tables eventhough the application is running.

I can send you the code if you would like to have a look at it.Please help me.
Title: Great Article!!!!   
Name: madhu
Date: 2008-06-05 11:50:22 AM
Hi Abhishek,
This is a wonderful article,I am new to windows service applciations and for the last week or so I am breaking my head to run an applciation which stopped working even though the service is running.I will try the tips and suggestions made in this article and see if it works.Anyways thanks for the great article.
Title: how to make automated servicebuilt in vc++ from starting status to started   
Name: usha
Date: 2008-05-15 4:14:02 AM
hey abhishek thanks for tutorial really handly ..would you please tell me wat the porblem would be in this scenirio automated services bulit in vc++ wen rebooted has starting status but i want it as started like any other normal automatic services in windows xp
Title: It runs for sometimes [3- 10] times and stops   
Name: Robin Thomas
Date: 2008-05-14 5:21:18 AM
Solution ...

Enter this as class variables...

protected const iTIME_INTERVAL As Integer = 60000
protected System.Threading.Timer oTimer;
Title: Great Article   
Name: Steve Harclerode
Date: 2008-05-09 2:50:50 PM
This was very useful for me, thank you.
Title: Excellent   
Name: Simon Richardson
Date: 2008-05-08 5:46:21 AM
Just wanted to say that this was an excellent article. Paricularly the bit about the setup project. Until I saw this it was nothing but CMD windows for me. Thanks a lot
Title: Service install uninstall work find to me   
Name: Abhishek Singh (Author)
Date: 2008-04-29 3:52:31 AM
Hi EAmez,
I tried to regenerate your problem by installing,starting, attaching debugger, stopping and uninstalling service. In my case everything was normal.
Still if you want then you can send me your service source and setup project (if it is not your company project) to me, which i can use for test.
Title: Reply to: How do we edit and redeploy?   
Name: EAmez
Date: 2008-04-28 10:33:57 AM
Hi Abhishek ,

When I say that process is still running after uninstallin service, I mean that if I open Task Manager I can see a process with the name I've given to mine. I assume that it's an instance of my process that's still running, but why is it there when I've uninstalled? And how to kill it?

I've tried what they say in a link before (
but it gives me an error saying there doesn't exist a process with the given name.
Title: Reply to: How do we edit and redeploy?   
Name: Abhishek Singh (Author)
Date: 2008-04-28 7:38:30 AM
how do you check that processing is still running after uninstalling service. Do you mean, process means .exe of service?
Title: How do we edit and redeploy?   
Name: EAmez
Date: 2008-04-28 4:05:49 AM
Oh, Abhishek, I was writing my previous message while you published yours.

I've configured the service to initiate it manually, so before uninstall I stop it manually. Then the steps I follow are: build code, build setup project, install setup, init service through the services console, attach the process to the debugger, stop the debugger when I finish, stop the service through the services console, uninstall the service. Install and Uninstall actions are done through the IDE (VStudio 2005).

As I said before, after these steps I can re-run the services again, so I don't really have a problem with the service. But I'm worried about this process that keeps running "forever".
Title: How do we edit and redeploy?   
Name: EAmez
Date: 2008-04-28 3:57:02 AM
Sorry, I forgot to say that in my case, when I try to reinstall the service it works and lets me work with it.
Title: Reply To: How do we edit and redeploy?   
Name: Abhishek Singh (Author)
Date: 2008-04-28 3:49:11 AM
Hi EAmez,
Is my previous commnet helpful to you? I wonder if you are getting some different issue. In your case check the status of service whether it has gone to "Disabled". If so than you may to manually remove it.. before installing again. or you can change the service name to some other and forget the disabled service.
Let me know if anything else. Thanks.
Title: eply on : How do we edit and redeploy?   
Name: Abhishek Singh (Author)
Date: 2008-04-28 3:41:46 AM
Hi James,
I had also faced this problem serveral times.I think the problem with service uninstallation is not a big problem actually. What you need to do after uninstallation of service is - EITHER refresh the service console window (then you will see service will disappear) OR close the service console window before trying to install service in fresh. I believe this a minor bug left by Windows. If you keep console window it doesn't automatically removes that service from the list.
Hope it will help you. Thanks.
Title: How do we edit and redeploy?   
Name: EAmez
Date: 2008-04-28 3:32:09 AM
I'm having the same problem than James Collet. I think I've done nearly the same things. I've installed the windows service through the IDE, run it through the services console, debugged it, stopeed the debugger and then uninstalled de service through the IDE. I first thought that the service was still running because I've put a Thread.Sleep instruction, but after restarting the workstation it's still running.

And if I try to kill the proccess through the tasks administrator it "ignores" me (it seems like if the process automatically restarts after killing).

¿Why does this happens?
Title: Reply on : How do we edit and redeploy?   
Name: James Collett
Date: 2008-04-25 5:22:30 AM
No, I don't think that is the issue or the solution.

I can install the service, form scratch, from the VS 2005 IDE, with no errors.

Then I shut down the IDE.

The Service turns up in the Services console. I start the service and it runs, writing out the lines of text to the text file.

Then I stop the service from Windows and open the solution in the IDE again.

I right click the setup project and choose "Uninstall". It appears to execute this process successfully, but if I open the Wondows Services console again, I see that the service is still there, albeit not running.

Just to be ultra careful, I restart the workstation, just in case. But the service is still installed.

I open the solution in the IDE again and attempt to uninstall the service again, but I get the error message: "This action is only valid for products that are currently installed."

So next I try to install the service again, but I get the error message: "The specified service already exists."

This strikes me as Classic Microsoft crap. Or Maybe I am being stupid. Experience suggests the former. If anyone has a clue what is going wrong here I should be most grateful to know.

I have just found this link, which identifies the scenario. It might lead to an understanding of what I have done wrong:-

P.s. Incidentally, when the service ran, it worked for a few hours and then stoipped writing to the file. I think this might be another issue with he code.
Title: Reply on : How do we edit and redeploy?   
Name: Abhishek Singh (Author)
Date: 2008-04-25 2:00:44 AM
Hi James, you are getting error "it is not allowed to install an already-installed service." while trying to install the service second time, because you missed to uninstall it before that. My strong suggestion would be : Always uninstall the service with the same setup used to install the service, then only you should update the setup. Most of the time you can not uninstall the service with the setup which got updated with modified code in service.
I think you got the answer.
Title: Reply on: Code stops executing but service is still running   
Name: Abhishek Singh (Author)
Date: 2008-04-25 1:54:04 AM
Craig, I think you need to check your db connection and datareader type of object used in the code properly. Check if you are closing connection and datareader in each call, if you are initializing in the trigger function itself. I think i need to see your code to determine the exact reason.
Title: Good Work   
Name: Abhishek
Date: 2008-04-24 6:22:39 PM
Good Work. This is a nice Article to get started with Windows Services.
Title: How do we edit and redeploy?   
Name: James Collett
Date: 2008-04-24 12:51:05 PM
This article seems very comprehensive.

I wanted to change the operation of this service so that it appends a vbNewLine at the end of the message that it writes to the text file.

It's easy enough to change the source and rebuild, but how to redeploy this change? I tried to Uninstall and re-install from the IDE but this resulted in a message saying that I was not allowed to install an already-installed service. Nor could I find a way to delete the service from the Services Manager.

This must be Kindergarten stuff but this baby service coder ain't gettng it.
Title: Code stops executing but service is still running   
Name: Craig
Date: 2008-04-22 3:39:22 PM
I had no issue copying your code in this article and getting a service up and running. However, once I changed the code to do some other database related tasks, it randomly stops executing. The odd part is, the serivce is not stopping? The code will run anywhere from 10 to 20 times and then just stops executing?

I have tried adding break points on the code as oulined in the article and there are no logic errors in the code itself.

Anyone know why this may be happening or how to fix it?

Thank you!
Title: FeedBack   
Name: Jony
Date: 2008-04-18 2:46:42 AM
Good explanation on windows service.
give more questions and ans.
Title: Nice done   
Name: JayBe
Date: 2008-04-08 4:15:23 PM
Thank you very much for this nice walkthrough, finally somebody who's working with the newer stuff, most of the walkthroughs are for older Visual Studio's and don't usually work good.

Thanks again
Title: Reply to: How make a windowservice for continue data storing in table   
Name: Abhishek Singh(Author)
Date: 2008-03-14 10:08:09 AM
hi vidyashankar,
i think your doubt is very simple to clear. Windows services periodication triggers a user defined funtion using Timer object. Like in the above example you can see code:
Dim tDelegate As Threading.TimerCallback = AddressOf EventAction
Here that triggerable function is EventAction which i have given name in this article.
again you can find the difinition of that function:
Public Sub EventAction(ByVal sender As Object)
'write your logic for one trigger here
End Sub
Put your processing login/code in this functtion.
You can set timeinterval for each trigger for the service. Timer takes it in millisecond.
That's it. Then build the setup project,install the service and start.. see it will work as you want.

Hope it is clear to you. Thanks a lot.
Title: How make a windowservice for continue data storing in table   
Name: vidyashankar
Date: 2008-03-14 5:42:04 AM
Ur article is nice i have one doubt in that concept that is how is continue getting data from meter reading device and stored in data base
Title: Reply to :Timer Not firing indefinitely   
Name: Lee
Date: 2008-03-11 8:00:22 AM
I had the same problem, turns out there are two types of timer in vs2005, one is in, the other is in system.timer.timer. If you click on the timer and look at it's properties you will see which one you have. You need the second one else it will not work.
Title: Reply to :Timer Not firing indefinitely   
Name: Abhishek Singh (Author)
Date: 2008-02-28 7:15:57 AM
hi pradip,
Opening log file in NOTEPAD should not cause suce type of error. Reason for timer not firing properly could be something else. Check you time interval properly. as you know timer object accepts time interval in milliseconds.
Hope you will get it right.
Title: Timer Not firing indefinitely.   
Name: Pradip
Date: 2008-02-28 2:23:19 AM

Very Nice article to start understanding windows service.

I am also working on some kinds of windows services. So as a base I created the above sample windows service and it worked fine. However The timer was not firing properly.

For checking whether the service is running properly or not i used to open the log text file regularly. I think this is the reason that the service can't put entry into the log file. It throws an exeption and so the service terminates.

I am not sure whether this is the exact reason.

Please comment if am worng or you have to add some points into it.
Title: Configuration Files   
Name: Paul Kuczwara
Date: 2008-02-27 5:01:26 PM
Hi great article. I am new to writing window services. I tried the demo, works great. I am writing a service to pull our active directory users and refresh the cache on automatic basis. But how do I pass application variables into the service? Maybe I am out in left, but if you can help it would be appreciated.

Title: Developer   
Name: Cheryl
Date: 2008-02-26 12:15:02 PM
Do you have any instructions for C#
Title: Great Article   
Name: Andrew Murphy
Date: 2008-02-19 4:58:43 PM
Hi There,

Just wanted to say that this is great article! worked well for me. I have one sneeky tip that some people may find usefull.

If you would like to debug the process from the start process , if you add in a thread.sleep for a given time (say a minute), this should give you enough time to attach VS to the process. Remember to remove the sleep before you go to test/production :-)

Once again Great article
Title: Reply to :How to call a .net application exe in windows service   
Name: Abhishek Singh (Author)
Date: 2008-02-15 10:24:33 AM
Hi Kavita,
I can suggest you that you try by also adding code to execute the exe from OnStart() function. this will be only once. Thereafter you event method will be executing the to run exe.
Title: How to call a .net application exe in windows service   
Name: Kavita B
Date: 2008-02-15 2:31:52 AM
Hi Abhishek,
I am trying to call .net application exe in windows service using appdomain, i have created one object and call the exe using obj.executeassembly(path)but when i am starting the service, it is giving message as "The service on local copy started and stopped, some services stops auto matically when they have no work"
can u suggest on which event exe should call.

Title: Debugging Windows service   
Name: Abhishek Singh (Author)
Date: 2008-02-14 1:22:59 AM
Hi Kavita,
As i have mentioned in the article we can not debug the onstart() of the service since service comes in process list after startring. As the service must be started to attach its process to debugger, we need the service to be started in advance. Hence, normally we cannot debug the OnStart method of service. So you should put breakpoint out of onstart().
However, we can make an extra dummy windows service which should internally start the actual service. By attaching debugger to the process of dummy service we can step into the OnStart method of actual service to debug it.
Title: Debugging Windows service   
Name: Kavita B
Date: 2008-02-14 1:10:01 AM
Hi Abhishek,
I have attached the process and set the debug pointer then start the service still the breakpoint is not activated, and even after stopping the service the break poin for stop also not activated.
can u suggest any thing.

Title: how to debus windows service   
Name: Kavita B
Date: 2008-02-14 12:46:33 AM
Hi Abhishek,
can you suggest how to debug a windows service, i have installed service sucessfully and started the service but it is not giving the output, it seems service is not working at all, i need to debug the service onstart how should i go about it.

Title: service can't restart itself   
Name: Abhishek Singh (Author)
Date: 2008-02-13 2:05:53 AM
hi Kavita,
a windows service can't restart itself. howefer you can set it startup system as automatic to start it when machine restarts.
by the why do you want to stop and start the service. let the service running always and you do check to the db connection at each time interval. when db connection found then you can run the exe or any business processing you want.
Is it helpful? Let me know if any thing more i can do.
Title: How to create windows servise in visual studio 2005   
Name: Kavita B
Date: 2008-02-12 7:04:38 AM
I need to create a windows service which runs a application exe.While running this service should it check for the database connection, though i have handled the connection of databse in VB.NEt application, still this ser vice should check for the database connection and keeps running, in case of database connection failure it should stop and again restart, check after successful DB connection it should atart working.
How should i go about this,
Can u suggest.
Title: Love it!   
Name: Caroline Wright
Date: 2008-01-29 9:40:57 AM
Excellent Articule! Thanks!
Title: to run every minute   
Name: Abhishek (Author)
Date: 2008-01-22 1:33:22 AM
perhaps i did not understand you problem. i think you can easily set to run even function in every min as the code given in this article. service time interval you can set in millisecond (60000 for one min).
please give more details if it doesn't help.
Title: hay i am doing similar thing   
Name: hiren
Date: 2008-01-21 5:38:10 AM
hay i am using similar thing but i have some changes in my code in eventaction but that is not issue the issue is my service is started sucessfully but i set 1 min time interval, the process is being only two to three times only it itsn't run my code every one minute i want my task would be done every minute. how can i do? help me please
Title: Good work   
Name: Olegd
Date: 2008-01-16 5:28:59 PM
Thanks. It is very good article. Right to the point.
Title: Reply to :Running in a different system   
Name: Abhishek (Author)
Date: 2007-12-08 1:09:13 AM
hello sriram,
you can install and run windows server on any machine having windows OS. But that machine should have Microsoft .Net Framework 2.0 installed to run service developed in Visual Studio 2005.
Title: Running in a different system   
Name: Sriram
Date: 2007-12-07 3:21:04 PM
HI Abhishek,
Kudos to u!!Good work.

A small doubt!
I have created a windows service in one system.Will it be possible for me to run this service in another machine or server which does not have Visual Studio 2003/2005 or will i be able to run this/install and run this on a different machine at all?
Title: Re: a problem!   
Name: Abhishek (Author)
Date: 2007-11-21 1:57:45 AM
Windows service any how needs periodic (say for each 2 minutes) timer event to be raised. Now it depends upon you how you can check if it is business hours then only process the resource using functionalities. Here i mean service will be runnning though.
if you want not to raise unnecessary timer event unless it is business hours then you can skip using windows service and go for windows build-in scheduler to setup your functional program.
Hope my comments have some meaning for you. Thanks.
Title: cont:   
Name: shweta
Date: 2007-11-19 6:25:35 AM
when i said resources not being used, i meant when it has stopped resources shudn be used at tht tym.
Title: a problem!   
Name: shweta sharma
Date: 2007-11-19 6:21:42 AM
i have created a windows service. now i need this service to run only on buisness days n buisness hours. i have tried using timer, bt with it , it still consumes resources.
1) is thr any way to do this without resources being used.
2) how do i do it using timer class or nethn else for th matter, m doing this in
Title: Missing installation   
Name: Abhishek Kumar Singh (Author)
Date: 2007-10-09 3:05:40 AM
Hi Joel,
You might have not installed the VB part in .net. You can upgrade you IDE with the installers and add missing stuff.
Title: Template   
Name: Joel
Date: 2007-10-04 11:29:24 AM
Hi. This article looks like it will help me create the service that I need to create, but where do you get the VB Windows Service template? That is not an option in VS2005. There is a Windows Service template under C++, but I prefer VB. Thanx
Title: timer problem   
Name: paul
Date: 2007-10-04 4:30:36 AM
my code works fine when i put the code as class variable
protected const iTIME_INTERVAL As Integer = 60000
protected System.Threading.Timer oTimer;
Title: solution: only logs 5 times   
Name: Nicole James
Date: 2007-10-03 9:24:55 PM
If you make these two variables class variables (C#):

protected const iTIME_INTERVAL As Integer = 60000
protected System.Threading.Timer oTimer;

It will write to the log file as long as the service runs.

Nice article!

Title: Re: Problem in comment   
Name: Abhishek Kumar Singh (Author)
Date: 2007-09-10 2:30:01 AM
Sameeullah Daris,
if possile send me your c# code to my mail id-
I will add your c# code in the this article.
Title: Problem in comment   
Name: Sameeullah Daris
Date: 2007-09-09 11:00:43 AM
Mr Abhishek Kumar Singh
I like your article too much .

I added c# code for this but unfortunatily that doesn't appear here ..
I don't know why this site is not showing my comments of C# code ...

good luck ...
Title: Well Done Mr Abhishek Kumar Singh (Here is C# Code)   
Name: Sameeullah daris
Date: 2007-09-09 10:53:36 AM
Title: Re: The timer stops firing   
Name: Abhishek Kumar Singh (Author)
Date: 2007-09-03 6:20:52 PM
Ilya, first of all I am really very happy to get such a detail and informative feedback by you. :-)

Your comment hints that you have done lot of findings about the services. Yes you are right. The same issue were with me while I was deploying services on almost 10-12 live servers. Few had Windows 2003 server and other windows server 2000.

Then service stop issue came to me also. I did lot of search for the reason and nowhere i could found the exact reason and solution. As all reasons were assumable. Then i did some experiments in the code (like changes in logging system) and finally i could make the service to run on all service without any issue. However still i can't say what made it to work.

May be i am with misconception that my lucky experiment in the code made it to work but in other way i also think that may be it was because of continuous updates of OS or service packs on live servers. :-)

Whatever be the consequences, thanx for your appreciation. Hope to get your valuable suggestion always.
Title: The timer stops firing   
Name: Ilya
Date: 2007-09-03 4:15:08 PM
Abhishek, first of all, thanks so much for your excellent article! I was looking for a quick example on how to use timers within a service and found so much more in your article: step-by-step instructions, deployment and debugging tips. Excellent!!!

However, I am experiencing the same problem as Rick, Mark, Manish and others reported below, and apparently many-many others reported elsewhere on the Internet.

Abhishek, to make sure I have not introduced any new problems, I am running your code attached to this article. I am even using your installation files (built by you on 06/08/2007). It seems to work fine on my Windows XP notebook (though I didn't to run it for too long), but when I install the AuthorLogService on a Windows 2003 SP2 Server, the service only works for a few minutes. After that it simply stops firing EventAction without any apperent reason. The service continues to run, but no further updates to AuthorLog.txt take place until I stop and start the service again.

Abhishek, I suspect it's not a fault of your program, but rather some Microsoft's bug. I just wish someone could find a quick solution!

Title: A suggestion   
Name: Abhishek Kumar Singh(Author)
Date: 2007-08-30 2:54:47 AM
Always take care in disposing object. Don't rely on automtic garbage collection in the service. You should dispose it through code.

Do proper thread/event management in the service. Check if threads are being killed or not.
Title: Re: adding a web reference   
Name: Abhishek Kumar Singh(Author)
Date: 2007-08-30 2:48:44 AM
Hi Alexander,

1. Surely! you can reference and consume web services from inside Windows service project.

2. If you are not able to use debugger try to follow as i mentioned in this article.. it should work.

3. If service works only at start/restart then.. check your code properly if the even is being fired properly. You can also check your event handler code in windows application and if it works there then place it into service. If still get problem there are other technical issue based on service which may cause this like OS related, service event time related, event log access issues etc. You need to go through forums properly and do a bit RnD.
Title: adding a web reference   
Name: Alexander Gonzalez
Date: 2007-08-29 10:45:53 AM
It is possible to add a web service to it?, I need to do some web service consuming and this seems like a good way to do it, I have a problem however, the debugger isn't working and the service only works when I start / restart the windows service. Thanks
Title: Thanx Luís   
Name: Abhishek Kumar Singh (Author)
Date: 2007-08-27 5:18:47 AM
Thanx a ton Luís.
Title: GREAT tutorial!   
Name: Luís Miguel Silva
Date: 2007-08-23 7:16:39 PM
Dear Abhishek,

Thank you for a great tutorial ;o)

It really helped me work out how to do a windows service with an installer!

The documentation at MSDN is A NIGHTMARE but with your tutorial i was able to do everything at first!

Best wishes from Portugal,
Luís Silva
Title: OS   
Name: Rick
Date: 2007-08-23 1:36:45 PM
I am running the service on xp. The time interval is 60 seconds.
Title: Re: Re: Threading.Timer   
Name: Abhishek Kumar Singh (Author)
Date: 2007-08-23 1:46:05 AM
Rick, As you can test the setup given with this article. It runs fine. Howerver in your case service halts after sometime. BTW on which OS you are running the service, XP?
what is the time interval for timer?
Title: Re: Threading.Timer   
Name: Rick
Date: 2007-08-22 12:04:32 PM
I am using vs 2003. Not sure if that makes a difference.
Title: Re: Threading.Timer   
Name: Rick
Date: 2007-08-22 12:01:58 PM
I found out when I run your example subs, it only runs 5 times, then it never runs again.
Title: Re: Threading.Timer   
Name: Rick
Date: 2007-08-22 11:09:21 AM
It's System.Threading.Timer object. It's wierd that if in the OnStart I set it to call your example sub that writes to the txt file, it works fine every interval. But when I change it to
Dim tDelegate As Threading.TimerCallback = AddressOf taskAction (taskAction is my service that does some maintaining job on the remote db server). This only works one time when I started the service, then nothing happen when each interval ends. I thought it's my db code has problem, but I debug it with nothing wrong. My last try would be comment out each sub/line of code to see what cause the problem.
Title: Threading.Timer   
Name: Abhishek Kumar Singh(Author)
Date: 2007-08-22 1:38:23 AM
Are you sure you are using System.Threading.Timer object not windows timer? Windows timer will not work properly in service. Check it and reply me.
Title: only runs the first time   
Name: Rick
Date: 2007-08-21 2:40:05 PM
Hi, I have the similar problem. My windows service only runs the first time when I start the service. when the interval time ends, it does nothing. It's still started in the service window. My service does something different, it's for maintaining the database. I read all the posts and made changes, but still the same. Any idea?
Title: Check file Closing   
Name: Abhishek Kumar Singh (Author)
Date: 2007-08-19 3:30:15 PM
Hi Mark, I guess you are not closing/disposing the file object. Service creates new timer thread at each call. So please check if file object is closed just after each file write.
Title: Stops writing to file...   
Name: Mark
Date: 2007-08-15 4:06:30 PM
I loved the tutorial. However, the service stops writing to the file after a few minutes. I've checked to see if the service is still running and it is. I also added the following lines to the end of the OnStart() method:


I get the same result. Any ideas?
Title: Author Reply for : Will it work infinitely   
Name: Abhishek Kumar Singh
Date: 2007-08-09 2:24:00 AM
Hi Manish, thanx for your try. Yes, the windows service should run infinitely (untill it is being stopped or system shutdown).
If your service stops writing into file.. please check at that time whether service status is showing "Started" or not (do refresh before that).
POSSIBLE SOLUTION: Please add following lines of code at end in OnStart() method and then rebuild service and setup.


Now install and test the updated service and please let me know if it works.

Title: Will it work infinitely   
Name: Manish
Date: 2007-08-08 1:53:52 AM
I am using this window service & this service is working fine.I set the interval of this service to 1 sec, but this service stop writing on the text file after 22 seconds, but i want to use the service which will write to the txt file infinite times.
can i use this in windows service.
Title: Thanx Prabhu and MHarr and .. OnStart() debug trick   
Name: Abhishek Kumar Singh
Date: 2007-08-07 3:49:52 AM
Thanx Prabhu for the appreation. Special thanx to MHarr for sharing another trick to debug OnStart() of a service. I explore it bit more here.

Guys! I tried it and .. it worked :)
We just need to put

#If DEBUG Then
#End If

as the initial statements in OnStart() method of the service. Then you build the service and setup in DEBUG mode and then install the service. Now START the service, it will show you the JIT debugger window and choose YOUR SERVICE PROJECT IN VS 2005 in that. It will move to your service project and will step into the code. Thereafter you can debug entire service. IF YOU DON'T SEE VS 2005 IN THE LIST YOU NEED SET VS 2005 TO WORK AS DEBUGGER THROUGH ITS IDE.

Thanx to all.

Title: Fantastic Article   
Name: Prabhu R
Date: 2007-08-04 2:48:07 AM
Dear Abishek,
This Article was Really Superb.. It would be very useful one who are all trying to learn about windows service..
Good work.. Keep Doing..

Prabhu R
Title: Debug OnStart method   
Name: MHarr
Date: 2007-08-03 11:06:50 AM
To debug OnStart event in service, put this code at or near the beginning of the OnStart method:

#If DEBUG Then
#End If

The "If DEBUG" will start debugging only if the program was compiled for Debug mode, not Release mode. This way it will launch the debugger (Visual Studio) and break execution, but not affect your production release that you compile in Release mode.
Title: great presentation   
Name: Mac Kalvin
Date: 2007-08-03 9:27:25 AM
I really liked the way u described. Good Wrok appricated
Title: Soln: Service nont appearing in service manger   
Name: Abhishek Kumar Singh
Date: 2007-08-02 3:42:49 PM
Hi Rose, to get your service after successfull installation you need to refresh the list(no need if you are opening SCM window after installation). If still you don't get your service there then check your service name properly. Note that .msi file name and service name may differ.

And to get the .msi file in Debug folder- choose comilation mode as "Debug".

Title: great presentation   
Name: kudox
Date: 2007-08-02 6:29:42 AM
woderful article as it helped addresed my problems. But I find it very hard to debug my service. how do i use a dummy service to start the real service so that i can debug the main service.
Title: Allowing service to interact with desktop   
Name: Sharad
Date: 2007-08-01 8:39:34 AM
Hi Abhishek,
Nice and very useful article. I am running this service as local user (logged-in) and also want it to allow interaction with desktop. But it looks like desktop interaction is allowed only for local system user. Any suggestion how to make the service allow interaction when run through local user.
Title: Thanks for Nice Article   
Name: Bangaru
Date: 2007-08-01 8:15:02 AM
Thanks alot for your article
This is very much useful and explanatory in a simple way
Great Job

Title: Service nont appearing in service manger   
Name: Rose
Date: 2007-08-01 6:52:09 AM
I tried the same but the installed service is not showing up in installed Services
Event log has been entered as installation is sucessful.
And .msi file is created only in Release folder
Could u help?
Title: User Account running the service   
Name: Sharad
Date: 2007-07-31 9:52:26 AM
Title: C# version   
Name: Bing
Date: 2007-07-30 11:27:39 AM
Any chance you can provide a C# version of this? Thanks for your good work anyway.
Title: uninstalling service   
Name: Abhishek Kumar Singh
Date: 2007-07-27 1:30:24 AM
Hi Mayank... when you uninstall service.. it removes the the service too. The service list in not being updated automatically. You need to refresh the list. Then the service from the list will disappear.

One more thing.. if the SCM window is open and successfully uninstalled the service ... then reinstallation of service might fail if you don't refresh the list.
If it doesn't solves your problem let me know. thanx.
Title: Good Article   
Name: Mayank
Date: 2007-07-25 6:28:56 AM
I tried that and it is working when I install, but when i perform uninstall, it should remove service too. But It does not work with current, can we do any thing to uninstall this.
Title: Awesome!   
Name: Dean S
Date: 2007-07-23 3:30:12 PM
Very well done sir...
Title: very nice   
Name: janmejaya swain
Date: 2007-07-18 5:24:37 PM
you have done a perfect job that might help the follower for further advancement
Title: good job   
Name: Sergi
Date: 2007-07-18 3:54:20 AM
congratulations from Barcelona.
Title: Tech Lead   
Name: Mostafa arafa
Date: 2007-07-16 5:15:02 AM
Thanks alot for this useful article.

i put its reference in my blog :)
Title: Very Nice Article   
Name: Chandrakanta Kar
Date: 2007-07-13 8:11:49 AM
It is very Necessary for developers those are developing Applications using VB.Net. Thanks Abhishek
Title: Simple and complete   
Name: Simon Stewart
Date: 2007-07-13 12:11:31 AM
Thanks very much, Abhishek.

This is a great article to keep as it clearly runs through all one needs to know to get a service up and running.
Title: good   
Name: Fernando Esteves
Date: 2007-07-12 9:37:40 PM
i'm brazilian and i'd like your article, very very good, and sorry my strong write english...
Title: Thanks to almonit and priyanka   
Name: Abhishek Kumar Singh
Date: 2007-07-11 3:21:21 PM
Hi almonit and priyanka : A melody thanks to you for appreciation and feedback.
Title: Nice article   
Name: priyanka
Date: 2007-07-11 9:37:21 AM
Hello Abhishek,
Its a nice article. I enjoyed reading...
Title: well done!!   
Name: almonit
Date: 2007-07-11 7:12:00 AM
vwery good & useful!

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-23 7:56:22 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search