How to Keep a .Net Console App Running

How to keep a .NET console app running?

you always want to prevent using while loops, especially when you are forcing the code to recheck variables. It wastes CPU resources and slows down your program.

I would definitely say the first one.

how to keep a simple c# console app running after end of the application.

Just use Console.ReadKey();

Console.Write(yourOutput);
Console.ReadKey();

How to keep Console application open and rerun code every 5 minutes?

I suggest to use:

while(true)
{

//Your Code Here
Console.WriteLine("Hello World");

int milliseconds = 300000; //300000 milliseconds = 5 minutes
Thread.Sleep(milliseconds);

}

How can I keep a Console App open when all processes are running on separate threads in c#?

You need to join your threads before exiting.

static public void Main()
{
/*
Existing code goes here
*/

//Before exiting, make sure all child threads are finished
foreach (var thread in connectionThreads) thread.Join();
}

When your program calls Thread.Join, it is telling the operating system that it needn't schedule the main thread for any timeslices until the child thread is finished. This makes it lighter weight than other techniques, such as busywaiting (i.e. running a while loop); while the main thread will still hold onto resources, it won't consume any CPU time.

See also When would I use Thread.Join? and c# Waiting for multiple threads to finish

How to keep a .NET console app running without looping?

There's an event on Console class that will help you detect that user pressed control+c

Console.CancelKeyPress += myHandler;

void myHandler(object sender, ConsoleCancelEventArgs args)
{
// do something to cancel/interrupt your jobs/tasks/threads
}

This event is raised when user presses Ctrl+C or Break keys. IIRC, the event is invoked asynchronously from the Main() thread, so you will get it even if your "Main" function wandered deep into some other code and is grinding something.

I mean, if your main thread sleeps in Task.WaitAll, you will still get that event if keys are pressed.

Another thing is, how to cancel your tasks. That depends on how your long running tasks are organized. Probably CancellationTokenSource will be your friend if you use Tasks, Threads or some your own things. Tasks support CancellationTokens/Source. You can pass the token as one of arguments to the Factory or Task.Run or similar task-creating methods. However, you also need to ensure that the token.ThrowIfCancellationRequested() is used in the tasks' inner code. If you use some libraries to work for you, they may happen to not support CancellationToken, then you will need to research how to cancel/interrupt them properly.

How to keep console window open

You forgot calling your method:

static void Main(string[] args)
{
StringAddString s = new StringAddString();
s.AddString();
}

it should stop your console, but the result might not be what you expected, you should change your code a little bit:

Console.WriteLine(string.Join(",", strings2));

Is there a better way to keep a console app from returning besides using while?

If you just need to wait until the launched process exits.

This could be written in your Timer.Elapsed event

Process[] p = Process.GetProcessesByName("yourprocessname");
if(p.Length > 0)
{
p[0].WaitForExit();
Console.WriteLine("Finish");
}
else
{
// Do other things
}

EDIT
Looks that this could be tried. I am not sure about threading issues here so waiting for someone more expert that points to the weakness of this approach

static Timer tmrCheck = new Timer();
static bool waiting = false;
static void Main(string[] args)
{
Initialize();
Console.WriteLine("Press enter to stop the processing....");
Console.ReadLine();
tmrCheck.Stop();
}

static void Initialize()
{
tmrCheck.Elapsed += (s, e) =>
{
Console.WriteLine("Timer event");
if(!waiting)
{
Process[] p = Process.GetProcessesByName("yourprocessname");
if(p.Length > 0)
{
waiting = true;
Console.WriteLine("Waiting for exit");
p[0].WaitForExit();
Timer t = s as Timer;
t.Stop();
Console.WriteLine("Press enter to end application");

}
}
else
{
// other processing activities
}
};
tmrCheck.Interval = 10000;
tmrCheck.Enabled = true;
}


Related Topics



Leave a reply



Submit