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
>
> 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..].
[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
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
What problems if I may ask?
Willy.
> | 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
Willy.
"Randy A. Ynchausti" <randy_y...@msn.com> wrote in message
news:uSgEsm6K...@TK2MSFTNGP15.phx.gbl...
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.