I want my program to continuously wait for a trigger in order to perform the next task.
- One option is pooling: using an infinite loop e.g. while(true)
- 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.
Unless you’re actually waiting for something to happen on a TCP/IP port, you shouldn’t (ab)use
Listen.An efficient method is
AutoResetEventthat 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.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: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.