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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:51:54+00:00 2026-06-13T17:51:54+00:00

I need to do some operations on a certain interval (e.g. from 5 to

  • 0

I need to do some operations on a certain interval (e.g. from 5 to 5 minutes in a loop) but need to be able to fully stop the thing whenever I want (on push of a button).

I was thinking into using a Timer class but events might fire even after the timer is stopped.

How can I have some code running on a timer and still be able to immediately bring everything to a complete stop?

Just so I am properly understood: By complete stop I mean that events stop and I can dispose of objects like the timer itself etc. I am not asking how to avoid having side effects from unexpected events that are fired after the timer is stopped!

  • 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-13T17:51:56+00:00Added an answer on June 13, 2026 at 5:51 pm

    Answer to this question depends a lot on a type of your operations.

    Best scenario is to run a thread with a loop and listen to abort event.

    static AutoResetEvent abort = new AutoResetEvent();
    Thread worker = new Thread(WorkerThread);
    
    void MainThread()
    {
      worker.Start();
      Thread.Sleep(30000);
      abort.Set();
    }    
    
    void WorkerThread()
    {
      while(true)
      {
         if(abort.WaitOne(5000)) return;
         // DO YOUR JOB
      }
    }   
    

    When you call abort.Set() from another thread, this one will exit.

    But if your code is long running, you won’t be able to exit until job is done.
    To exit immediately you will have to abort thread, but this is not too wise because of resource consumption.

    Alternatively, if your operation is long running (let’s say you are going through long array), you can check “abort” event state from time to time (every iteration of loop, for example) like this abort.WaitOne(0).

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

Sidebar

Related Questions

I need to perform some operations on a 2D array of values read from
I need to perform some operations on all folders within a file share which
I need to do some registry operations & other system operations for that i
Need some quick advice I am trying to access a object array but I
Windows displays UAC prompts on the secure desktop when certain security-related operations need to
I need to write some code that applies to only certain fields within our
I'm designing a web service using Sinatra and I need to perform certain operations
i'm developing an application that need to do certain operations right after the installation
I need to do some special operation for the last element in a list.
Need some regular expressions help. So far I have my code working to allow

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.