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

  • Home
  • SEARCH
  • 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 8912955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:20:38+00:00 2026-06-15T04:20:38+00:00

How to make a timer which forces the application to close at a specified

  • 0

How to make a timer which forces the application to close at a specified time in C#? I have something like this:

void  myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    if (++counter == 120)
        this.Close();
}

But in this case, the application will be closed in 120 sec after the timer has ran. And I need a timer, which will close the application for example at 23:00:00. Any suggestions?

  • 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-15T04:20:39+00:00Added an answer on June 15, 2026 at 4:20 am

    The first problem you have to fix is that a System.Timers.Timer won’t work. It runs the Elapsed event handler on a thread-pool thread, such a thread cannot call the Close method of a Form or Window. The simple workaround is to use a synchronous timer, either a System.Windows.Forms.Timer or a DispatcherTimer, it isn’t clear from the question which one applies.

    The only other thing you have to do is to calculate the Interval property value for the timer. That’s fairly straight-forward DateTime arithmetic. If you always want the window to close at, say, 11 o’clock in the evening then write code like this:

        public Form1() {
            InitializeComponent();
            DateTime now = DateTime.Now;  // avoid race
            DateTime when = new DateTime(now.Year, now.Month, now.Day, 23, 0, 0);
            if (now > when) when = when.AddDays(1);
            timer1.Interval = (int)((when - now).TotalMilliseconds);
            timer1.Start();
        }
        private void timer1_Tick(object sender, EventArgs e) {
            this.Close();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make a Windows Form Application which only shows a timer as:
I'm trying to make a time with Time.local which in my code is dynamic.
I want to make a time or date listener which for example invoke an
I am working on a timer which should after a certain time show a
This is going to sound like an odd request, but... I have a web
I am creating a fairly simple android application which is basically a timer, I
I'm trying to make an alarm clock in which you can set multiple times.
I want to make timer string with CCLabelAtlas. The problem is the character, :.
I'm trying to make a timer count down from 5 seconds to zero before
I'm trying to make a timer in c++. I'm new to c++. I found

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.