Dav
Dav

Reputation: 179

VB.NET Windows Service Main Code

I am trying to learn how a Windows Service works using vb.net and I have no clue where to put my main code. what I mean by main code is the code that needs to be run every couple of seconds. So far, I was able to write a text file using onStart method provided by the service its self. I have installed the service manually and made it run.

do i need to create some kind of threading that trigger the main code? and in which method.

Thanks.

Upvotes: 1

Views: 2675

Answers (1)

Kraxed
Kraxed

Reputation: 350

Use a Timer. They start an event on every tick. The tick can be modified to the length you want it to be.(Interval)

Here's a great article/tutorial on them: http://www.dreamincode.net/forums/topic/58033-using-timer/

Add a timer to your application by going to the toolbox and clicking a Timer and adding it to the form. Then go to the properties and add what interval you would like it to do an event(in milliseconds) the set the enabled property to true.

Click on the timer icon below the form designer and you will be directed to the code editor with the Timer_Tick event. Then in when I put the comment 'what you want it to do, this is the code that will be executed every tick(the interval what you selected, for example 1000 is a second)

Code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'what you want it to do
End Sub

If I am being unclear here is a tutorial: https://www.youtube.com/watch?v=6wWZIuOAyM4

Upvotes: 2

Related Questions