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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:20:22+00:00 2026-06-09T20:20:22+00:00

I am learning creating windows services and threading. I am using a library provided

  • 0

I am learning creating windows services and threading. I am using a library provided by fellow worker that aids in building threaded service but this is not giving me the knowledge at the basic level.

Lets say i will have a service that will be long running (little advance than the basic example available on the net), needs to wake up every 15 seconds and then perform its action (basically will be always running). Action involves looking for a status in the DB and then performing actions.

How should the following be handled in such cases:
1. disposing the thread
2. in cases where action takes longer to execute than the interval.

I have found the following example but i am having problems with the above 2 points. Please do keep in mind that the service will be running always.

http://www.java2s.com/Tutorial/CSharp/0280__Development/CreatethedelegatethattheTimerwillcall.htm

using System;
using System.Threading;

class MainClass
{
  public static void CheckTime(Object state) 
  {
    Console.WriteLine(DateTime.Now);
  }

  public static void Main() 
  {
    TimerCallback tc = new TimerCallback(CheckTime);


    Timer t = new Timer(tc, null, 1000, 500);

    Console.WriteLine("Press Enter to exit"); 
    int i = Console.Read();

    // clean up the resources
    t.Dispose();
    t = null;
  }
}

So in my example, what will go in
1. stop event
2. Does start event looks good?
3. what should happen if nothing found in the queue?
4. What if the actions take longer than the interval?

public partial class QueueService : ServiceBase
    {


        public QueueService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                TimerCallback tc = new TimerCallback(CheckQueue);


            Timer t = new Timer(tc, null, 10000, 15000); //first time wait for 10seconds and then execte every 15seconds

            }
            catch (Exception ex)
            {
        what should i be checking here and then also make sure that the threading/timer doesn't stop. It should still execute every 15 seconds
            }
        }

        protected override void OnStop()
        {

        what needs to go here...

        }

        private static void CheckQueue(Object state) 
    {
            ... Connect to the DB
        ... Check status
        ... if queue status found then perform actions
        . A
        . C
        . T
        . I
        . O
        . N
        . S
        ... if end

    }

    }

Thanks for looking!

  • 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-09T20:20:24+00:00Added an answer on June 9, 2026 at 8:20 pm
    1. Disposing the timer.
    2. Not completely. You need to declare the timer at class level otherwise it will be collected after few iterations.
    3. Nothing.
    4. Stop the timer before you check the queue and start it again after you finish with it. This way you won’t get into troubles of shared memory or other collisions.

      public partial class QueueService : ServiceBase
      {

      Timer timer;

       public QueueService()
       {
           InitializeComponent();
       }
      
       protected override void OnStart(string[] args)
       {
          try
          {
             TimerCallback tc = new TimerCallback(CheckQueue);
      
             timer = new Timer(tc, null, 10000, 15000);
          }
          catch (Exception ex)
          {
      
          }
      }
      
      protected override void OnStop()
      {
          if (timer != null)
              timer.Dispose();
      }
      
      private static void CheckQueue(Object state) 
      {
          timer.Change(Timeout.Infinite, 0);
      
          ... Connect to the DB
          ... Check status
          ... if queue status found then perform actions
          . A
          . C
          . T
          . I
          . O
          . N
          . S
          ... if end
      
          timer.Change(10000, 15000);
      
      }
      

      }

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

Sidebar

Related Questions

I've been creating a an application with C# using W2k8+IIS7/SQLExpress 2008/Web Service/Windows Client. I'll
i am using windows 7 for learning assembly language.I am using NASM for creating
I am considering creating e-learning SaaS solution so therefore I am evaluating using application
Yesterday I began learning WCF by porting an existing ASP.NET Web Service. Creating the
I'm currently learning rails by creating a simple project management app. I've gotten to
I'm creating a blog engine as a learning exercise and one particular problem has
First, I'd like to apologize, I'm still a beginner. For learning purposes I'm creating
Learning a lot in my few years of programming that the best projects are
I am creating a new rails app as a learning project. I have already
Hi I have been learning Java Swing for creating a chess game to practice

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.