Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:07:33+00:00 2026-05-16T07:07:33+00:00

I’m trying to write a small Windows Service in .NET 3.5, that check every

  • 0

I’m trying to write a small Windows Service in .NET 3.5, that check every 10mn or so if thee are new files in "C:\demofolder", then send e-mail out. So far, I made it to here like in the following code, then there’s error in Public Sub New()

Imports System
Imports System.Timers
Imports System.ServiceProcess
Public Class TestMyService

    '  A timer that controls how frequenty the example writes to the
    '  event log.
    Private serviceTimer As Timer

    Public Sub New()

        '  Set the ServiceBase.ServiceName property.
        ServiceName = "TestMyService Service"

        '  Configure the level of control available on the service.
        CanStop = True
        CanPauseAndContinue = True
        CanHandleSessionChangeEvent = True

        '  Configure the service to log important events to the
        '  Application event log automatically.
        AutoLog = True

    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' Add code here to start your service. This method should set things
        ' in motion so your service can do its work.
    End Sub

    Protected Overrides Sub OnStop()
        ' Add code here to perform any tear-down necessary to stop your service.
    End Sub

Public Shared Sub Main()

    '  Create an instance of the TestMyService class that will write
    '  an entry to the Application event log.  Pass the object to the
    '  shared ServiceBase.Run method.
    ServiceBase.Run(New TestMyService)

End Sub


End Class

I got the following error message:

Sub Main’ is declared more than once in ‘mcWinService.TestMyService’: mcWinService.TestMyService.Main(), mcWinService.TestMyService.Main()

Public Shared Sub Main()’ has multiple definitions with identical signatures.

Public Sub New()’ in designer-generated type ‘mcWinService.TestMyService’ should call InitializeComponent method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:07:34+00:00Added an answer on May 16, 2026 at 7:07 am

    Try to move

    ServiceBase.Run(New TestMyService) from Public Shared Sub Main()

    to

    Protected Overrides Sub OnStart

    then remove Public Shared Sub Main()

    Also, remove Public Sub New(), because you can set those properties from “Property Windows”. F7 to toggle from code to designer view.

    Update 1: Example of Windows Service for folder monitor

    Imports System.Net.Mail
    Imports System.Net
    Imports System.Timers
    Imports System.IO
    
    Public Class DemoFolderMonitor
    
        Private Shared timer As System.Timers.Timer
        Private Shared timerInterval As Integer
    
        Protected Overrides Sub OnStart(ByVal args() As String)
            ' Add code here to start your service. This method should set things
            ' in motion so your service can do its work.
    
            '  Use the EventLog object automatically configured by the
            '  ServiceBase class to write to the event log.
            'EventLog.WriteEntry(String.Format("DemoFolderMonitor Service starting."))
    
            ' Set the Interval  (1sec = 1000 milliseconds).
            timerInterval = 2000
            timer = New System.Timers.Timer(timerInterval)
            EventLog.WriteEntry("DemoFolderMonitor Service starting.")
    
            ' Hook up the Elapsed event for the timer.
            AddHandler timer.Elapsed, AddressOf WatchFolder
    
            timer.Interval = timerInterval
            timer.Enabled = True
    
        End Sub
    
        Protected Overrides Sub OnStop()
            ' Add code here to perform any tear-down necessary to stop your service.
            EventLog.WriteEntry("DemoFolderMonitor Service stopping...")
    
        End Sub
    
        Protected Sub WatchFolder()
    
            Dim watcher As New FileSystemWatcher
            watcher.Path = "C:\Demo\"
    
            watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
    
            'watch all file types.
            watcher.Filter = "*.*"
            ' Add event handlers.
            AddHandler watcher.Changed, AddressOf OnChanged
            AddHandler watcher.Created, AddressOf OnChanged
            AddHandler watcher.Deleted, AddressOf OnChanged
            AddHandler watcher.Renamed, AddressOf OnRenamed
    
            ' Begin watching.
            watcher.EnableRaisingEvents = True
    
        End Sub
    
        'Define the event handlers.
        Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
            ' Specify what is done when a file is changed, created, or deleted.
            Dim changeLog = "File: " & e.FullPath & " " & " | Action: " & e.ChangeType.ToString
            WriteChangeLog(changeLog)
    
        End Sub
    
        Private Shared Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
            ' Specify what is done when a file is renamed.
            Dim changeLog = "File" & e.OldFullPath & " " & "renamed to" & " " & e.FullPath
            WriteChangeLog(changeLog)
    
        End Sub
    
        'Custom action. You can either send an e-mail or write change to local file
        'In this example I write change to local file
        Private Shared Sub WriteChangeLog(ByVal changeLog As String)
    
            'Create a text file and write the change log to the text file
            Dim filename As String = DateTime.Now.ToString("hh-mm-ss") & ".txt"
            Dim writer As StreamWriter
            writer = File.CreateText("C:\ChangeLog\" & filename)
            writer.WriteLine("Datetime Log  at {0} Log Message: {1}", DateTime.Now.ToString("MMM-dd-yyyy @ hh:mm:ss tt"), changeLog)
            writer.Close()
    
        End Sub
    
    End Class
    

    For further related reading check:

    • Setup project for a Windows Service application
    • System.Timers Namespace
    • FileSystemWatcher Class

    Cheer!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.