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 9009833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:18:10+00:00 2026-06-16T02:18:10+00:00

This is my first window service that I am writing, I need some help

  • 0

This is my first window service that I am writing, I need some help in writing it, I am trying to use single thread so that one thread can start the service
and the other thread can take care of calling the functions that does the database work. I am also using a timer so that this service only runs once a day below is my code

The reason i am posting this question is whenever I tried to install this service, it is throwing an error saying “fatal error occure”, it doen’t give me any details.

public partial class Service1 : ServiceBase
    {
        private DateTime _lastRun = DateTime.Now;
        Thread workerThread;

    public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            ThreadStart st = new ThreadStart(WorkerFunction);
            workerThread = new Thread(st);
            serviceStarted = true;
            workerThread.Start();
        }
     protected override void OnStop()
        {
            // flag to tell the worker process to stop
            serviceStarted = false;

            // give it a little time to finish any pending work
            workerThread.Join(new TimeSpan(0, 2, 0));
            timer1.Enabled = false;
        }

     private void WorkerFunction()
        {
                while (serviceStarted)
                {

                  EventLog.WriteEntry("Service working",
                     System.Diagnostics.EventLogEntryType.Information);

                  // yield
                  if (serviceStarted)
                  {
                     Thread.Sleep(new TimeSpan(0, 20000, 0));
                  }
                  timer1.Enabled = true;
                  timer1.Start();
                }

               // time to end the thread
               Thread.CurrentThread.Abort();
        }


         private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                    if (_lastRun.Date < DateTime.Now.Date)
                    {
                        timer1.Stop();
                // does the actual work that deals with the database
                }

            timer1.Start();
            }
  • 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-06-16T02:18:12+00:00Added an answer on June 16, 2026 at 2:18 am

    There’s a few things to check:

    1. Be sure that you have configured the EventLog source correctly (MSDN). My answer to Windows service Started and stopped automatically, exception handling issue could be useful here also.
    2. It looks like you are using a Windows Forms Timer – these require a UI message pump, which you won’t have in a service (MSDN). You should instead investigate using the Timer class in the System.Threading namespace (MSDN).

    In particular, you may find the using a System.Threading.Timer will simplify your code a great deal as this object will manage a bit more of the plumbing for you.

    I would also advise against calling Thread.Abort(): it can be harmful and unpredictable, and in your case it doesn’t appear that you need to use it at all. See To CurrentThread.Abort or not to CurrentThread.Abort and
    http://msdn.microsoft.com/en-us/library/5b50fdsz.aspx.

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

Sidebar

Related Questions

So, essentially, we're writing a service-level application that can alter attributes of various user-level
I'm trying to create a popup window that only appears the first time the
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
I have a Windows Service that I am in the process of writing. I
I have an Activity that retrieves data from a web service. This data is
I'm writing a windows service that once started will run every X hours. The
I have some method that is invoked on Application_Start. And it starts on first
I'm new to writing Windows Services. I decided to write one that makes outbound
I'm using a thrid party Windows service that handles some automation tasks by running
Some background I am planning to writing a REST service which helps facilitate collaboration

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.