471,316 Members | 1,002 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to make a console app. keep running without using Console.Readline

Hello All,

These days I am working on a console application that will register for
certain events and do some processing based on the event notification.

Now, all the objects are being created in main(). Once the main exists
the program ends.

Here is what I want to do.

Is there any way of keeping the console application running without
using Console.readLine or Console.Read()? these methods work ok for a
while but then they start creating problems for the code thats running
behind the scene.

One more thing I cannot make this application as a window's service as
multiple instance of this console app will run in a round robin
fashion.

Any help in this regard will be appreciated.

Nadeem

Feb 5 '06 #1
8 4174
na********@yahoo.com wrote:
Hello All,

These days I am working on a console application that will register for
certain events and do some processing based on the event notification.

Now, all the objects are being created in main(). Once the main exists
the program ends.

Here is what I want to do.

Is there any way of keeping the console application running without
using Console.readLine or Console.Read()? these methods work ok for a
while but then they start creating problems for the code thats running
behind the scene. [Are you talking about do something like
while(true)
{
//do your stuff, waiting for some events.
}

One more thing I cannot make this application as a window's service as
multiple instance of this console app will run in a round robin
fashion. [ You can run multiple service in this way, just use some alias like
your MyService$InstanceA$ , MyService$InstanceB$ , I know MSDE/SQLExpess
use this way too..].
Any help in this regard will be appreciated.

Nadeem

Feb 5 '06 #2
Well, you can do this:

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Started");
System.Threading.Thread.Sleep(System.Threading.Tim eout.Infinite);
}
-- although it's pretty kludgy. As the other kind poster has mentioned, it
is very easy to have multiple windows services (Same executable) running
provided the exe's are in different folders and the ServiceName property is
different for each.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"na********@yahoo.com" wrote:
Hello All,

These days I am working on a console application that will register for
certain events and do some processing based on the event notification.

Now, all the objects are being created in main(). Once the main exists
the program ends.

Here is what I want to do.

Is there any way of keeping the console application running without
using Console.readLine or Console.Read()? these methods work ok for a
while but then they start creating problems for the code thats running
behind the scene.

One more thing I cannot make this application as a window's service as
multiple instance of this console app will run in a round robin
fashion.

Any help in this regard will be appreciated.

Nadeem

Feb 5 '06 #3
Do you really *need* a console application? Could you write a formless
windows app that calls Application,.Run() at the end of Sub Main?

Feb 6 '06 #4

<na********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| Hello All,
|
| These days I am working on a console application that will register for
| certain events and do some processing based on the event notification.
|
| Now, all the objects are being created in main(). Once the main exists
| the program ends.
|
| Here is what I want to do.
|
| Is there any way of keeping the console application running without
| using Console.readLine or Console.Read()? these methods work ok for a
| while but then they start creating problems for the code thats running
| behind the scene.
|

What problems if I may ask?
Willy.
Feb 6 '06 #5
nadeem_far,
| Hello All,
|
| These days I am working on a console application that will register for
| certain events and do some processing based on the event notification.


It sure sounds like you want a Windows Service to me.

Regards,

Randy
Feb 7 '06 #6
I don't see why this would be required? Also, the OP clearly stated that
this was not an issue.

Willy.

"Randy A. Ynchausti" <ra*************@msn.com> wrote in message
news:uS**************@TK2MSFTNGP15.phx.gbl...
| nadeem_far,
|
| > | Hello All,
| > |
| > | These days I am working on a console application that will register
for
| > | certain events and do some processing based on the event notification.
|
| It sure sounds like you want a Windows Service to me.
|
| Regards,
|
| Randy
|
|
Feb 7 '06 #7

Thanks for the ideas but I want to keep it console as I want to se the
progress on the console window. I dont want a UI as there is no user
interaction involved.

Also does any one of you know how to know if someone has hit the close
button on the command window. I want to know this as I want to give the
threads some time to complete the jobs currently running and then
shutdown cleanly.

Nadeem

Feb 10 '06 #8

<na********@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
|
| Thanks for the ideas but I want to keep it console as I want to se the
| progress on the console window. I dont want a UI as there is no user
| interaction involved.
|
| Also does any one of you know how to know if someone has hit the close
| button on the command window. I want to know this as I want to give the
| threads some time to complete the jobs currently running and then
| shutdown cleanly.
|
| Nadeem
|
You can register your own Console control handler by calling Win32 API
SetConsoleCtrlHandler using PInvoke.
Consider following snippet as a sample.

enum CtrlType {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private delegate bool EventHandler(CtrlType sig);

private static bool Handler(CtrlType sig)
{
bool handled = false;
switch (sig)
{
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
{
.. do whatever you need to do at close time (here for all
other events too), but keep in mind that the system will kill the process
when you fail to return within 30 seconds.
}
// return true when handled, this signals the system to remove
the process
handled = true;
break;
default:
// return false when not handled
return handled;
}
return handled;
}
static EventHandler _handler;

[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler (EventHandler handler,
bool add);

static void Main()
{
// install the handler
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
// ...
Willy.
Feb 10 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by jabailo | last post: by
5 posts views Thread by Richard Lionheart | last post: by
12 posts views Thread by Jarod_24 | last post: by
2 posts views Thread by SriBhargav | last post: by

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.