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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:34:23+00:00 2026-05-15T14:34:23+00:00

How do you make a event-driven program where the execution of the main flow

  • 0

How do you make a event-driven program where the execution of the main flow is suspended whenever an event is raised and handled, until the event handler finishes?

I created the following program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace EventTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer t = new Timer(2000);
            t.Enabled = true;
            t.Elapsed += new ElapsedEventHandler(TimerHandler);
            for (int i = 0; i < 10000; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                Console.WriteLine(i);
            }
        }
        public static void TimerHandler(object source, EventArgs e)
        {
            Console.WriteLine("Event started");
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                Console.WriteLine("Event "+i);
            }
            Console.WriteLine("Event finished");
        }

    }
}

The main method is supposed to print sequential numbers, and an event is fired every 2 seconds. During the event handling, the program is supposed to print “Event started”, “Event 1” to “Event 5”, then “Event Finished”, then returns.

I expected Main() flow to stop while TimerHandler() is called, however my output suggests that Main() and TimerHandler() both are run simultaneously

0
1
2
3
4
5
6
7
8
9
10
11
Event started
12
Event 0
13
Event 1
14
Event 2
15
Event 3
16
Event 4
Event finished
17
18
19
20
21
22
23
Event started
24
Event 0
25
Event 1
26
Event 2
27
Event 3
28
Event 4
Event finished
29
30
31
32
33

Is it possible to make the Main() method to stop while the event is handled?

  • 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-15T14:34:24+00:00Added an answer on May 15, 2026 at 2:34 pm

    You’re using System.Timers.Timer, which processes ticks on a background thread, typically using the thread pool.

    As such, yes, it will run simultaneously with the rest of your Main code.

    As for your second question, possible to get Main to stop, I need to know what the expected behavior is.

    If you want an entire tick in TimerHandler to run together, pausing Main while it does so, a simple lock would suffice:

    namespace EventTest
    {
        class Program
        {
            static object _Lock = new object();
    
            static void Main(string[] args)
            {
                Timer t = new Timer(2000);
                t.Enabled = true;
                t.Elapsed += new ElapsedEventHandler(TimerHandler);
                for (int i = 0; i < 10000; i++)
                {
                    for (int j = 0; j < 100000000; j++) ;
                    lock (_Lock)
                        Console.WriteLine(i);
                }
            }
            public static void TimerHandler(object source, EventArgs e)
            {
                lock (_Lock)
                {
                    Console.WriteLine("Event started");
                    for (int i = 0; i < 5; i++)
                    {
                        for (int j = 0; j < 100000000; j++) ;
                        Console.WriteLine("Event "+i);
                    }
                    Console.WriteLine("Event finished");
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 435k
  • Answers 435k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Search for POSIX threads, also known as pthreads. Tutorial Here May 15, 2026 at 3:40 pm
  • Editorial Team
    Editorial Team added an answer First, you should be using this for display the movies:… May 15, 2026 at 3:40 pm
  • Editorial Team
    Editorial Team added an answer 1) Strictly, you can combine the WCF library and the… May 15, 2026 at 3:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.