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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:13:39+00:00 2026-05-13T09:13:39+00:00

I want my program to continuously wait for a trigger in order to perform

  • 0

I want my program to continuously wait for a trigger in order to perform the next task.

  1. One option is pooling: using an infinite loop e.g. while(true)
  2. OR, Do TCP listen on a port with a relatively higher timeout.

I would like to know which one of these two techniques would be more efficient too keep my application alive?

I feel executing while(true) would be a killer and tcp listen might be a health option since the tcp listen would use hardware interrupt ?

Also, In .net winform applications we have the Application.Run() method that keeps the application alive. If anyone one knows what this method does internally pls share.

PS: I have already considered msmq option here ( which is equivalent to tcp listen ) but I do not want a dependency on msmq.

  • 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-13T09:13:39+00:00Added an answer on May 13, 2026 at 9:13 am

    Unless you’re actually waiting for something to happen on a TCP/IP port, you shouldn’t (ab)use Listen.

    An efficient method isAutoResetEvent that you signal when you want to trigger the processing of a task. This will make your thread sleep until it needs to do something, without any polling.

    class TaskProcessor
    {
        AutoResetEvent newTaskHandle = new AutoResetEvent(false);
        Queue<Task> taskQueue = new Queue<Task>();
        object syncRoot = new object();
    
        public void ProcessTasks()
        {
            while (true)
            {
                newTaskHandle.WaitOne();
    
                Task task = null;
    
                lock (syncRoot)
                {
                    if (taskQueue.Count > 0)
                    {
                        task = taskQueue.Dequeue();
                    }
                }
    
                // Do task
            }
        }
    
        public void AddTask(Task task)
        {
            lock (syncRoot)
            {
                taskQueue.Enqueue(task);
                newTaskHandle.Set();
            }
        }
    }
    

    This will probably raise the question how you can abort processing tasks. You can use more than a single WaitHandler (from which AutoResetEvent inherits) and wait for any of them to occur:

    WaitHandle[] handles = new WaitHandle[] { newTaskHandle, stopHandle };
    
    int signalledHandle = WaitHandle.WaitAny(handles);
    

    Alternatively, you could introduce a simple boolean and reuse the same event. That might actually be preferrable if you want to make sure all tasks are processed before stopping.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to have a look at humanized attributes in… May 13, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer If your server supports server-side include (SSI) then you use… May 13, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer Is the function fired under IE6? Because a common problem… May 13, 2026 at 10:05 am

Related Questions

I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
Q1. What are best practices for a writing a code that does not consume
How do I check if another application is busy? I have a program that
I have an open source Java application that uses Hibernate and HSQLDB for persistence.
I am not sure what the problem is but I keep receiving this error

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.