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

N

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.

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
 
J

John Sun

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..].
 
G

Guest

Well, you can do this:

[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Started");
System.Threading.Thread.Sleep(System.Threading.Timeout.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
 
C

Chris Dunaway

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

Willy Denoyette [MVP]

| 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.
 
R

Randy A. Ynchausti

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
 
W

Willy Denoyette [MVP]

I don't see why this would be required? Also, the OP clearly stated that
this was not an issue.

Willy.



| 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
|
|
 
N

nadeem_far

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
 
W

Willy Denoyette [MVP]

|
| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top