473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Never ending console app?

I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately. What
I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?

- Kristoffer -
Nov 16 '05 #1
6 5091
<"Kristoffer Persson" <hidden>> wrote:
I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately. What
I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?


Well, the easiest thing would be to just Wait on some reference or
other at the end of the Run method, and then when you want to exit the
app, make your timer Pulse the same reference.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

"Kristoffer Persson" <hidden> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately.
What
I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?

- Kristoffer -


One option is to use an AutoResetEvent.
AutoResetEvent ev;
public void Run()
{
ev = new AutoResetEvent(false);
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
ev.WaitOne(); //wait for event to be signaled
}

void OnTimer(...)
{
....
if(timeToExitProgram == true)
ev.Set(); //Signal event
....

}

Willy.
Nov 16 '05 #3
Try

Thread.Sleep(Timeout.Infinite);

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Kristoffer Persson" <hidden> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately. What I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?

- Kristoffer -

Nov 16 '05 #4
Try

Thread.Sleep(Timeout.Infinite);

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Kristoffer Persson" <hidden> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately. What I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?

- Kristoffer -

Nov 16 '05 #5
What about

Console.ReadLine();

Just make sure no one touches the keyboard!

Glenn

"Kristoffer Persson" <hidden> wrote in message news:<#F*************@TK2MSFTNGP10.phx.gbl>...
I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately. What
I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?

- Kristoffer -

Nov 16 '05 #6
Or, use ReadLine and then check for a very deliberate entry such as QUIT or
STOP RIGHT NOW or whatever.

In the long run, an application like this, once tested, should probably be
turned into a Windows Service.

--Bob

"Glenn" <cs****@blackwinter.net> wrote in message
news:dd**************************@posting.google.c om...
What about

Console.ReadLine();

Just make sure no one touches the keyboard!

Glenn

"Kristoffer Persson" <hidden> wrote in message

news:<#F*************@TK2MSFTNGP10.phx.gbl>...
I have a small console app that has a Run method like this:

public void Run()
{
m_Timer = new System.Timers.Timer(1000);
m_Timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
}

The problem is that the code obiously exits the Run method immediately. What I want is for my application to do nothing but handle timer events and
recognize when it should be shut down.

Some sort of message loop perhaps?

- Kristoffer -

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via...
3
by: talljames | last post by:
Hi all, Does anyone know how to end a Console.ReadLine() programmically rather than typing the enter key in C#? I have tried just setting the class object that contains this line to null, but...
9
by: Kenn Ghannon | last post by:
I've got an ASP.NET page with a counter subtraction routine in the Session_End method in the Global.asax.cs: protected void Session_End(Object sender, EventArgs e) { ulong curUsers; ...
12
by: D. Shane Fowlkes | last post by:
This is a repost (pasted below). Since my original post, I've double checked the system clock and set all IIS Session Timeout values to 10 minutes. Still ...the problem occurs. I've also...
0
by: daykirby | last post by:
I have a .net application that takes a couple of minutes to initialize, so I'm using the Application state to let it persist accross sessions. This is working well so far - after the first time the...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
2
by: barry.edmund.wright | last post by:
Hi All, I want to build a Find Statement on the fly. The code below starting with 'This Code Works! is fine but as I said I want to build the Me!cboSelect1 & "=" & rs(Me!cboSelect1) portion of...
4
by: wesbland | last post by:
>From my understanding, when a string is stored in VB.NET and you look at it in the debugger, it has a quote on both sides to signify that it is a string as opposed to a char or int or whatever. ...
34
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.