Arturo Martinez
Arturo Martinez

Reputation: 2583

Stop application from exiting

I'm working on a Windows application (not WinForms, not Console, not WinService), is just a project with an entry class. What is the best way, in .NET, to stop an application from exiting the Main method?

I know I can achieve this in console with Console.Read() or I can use EvenWaitHandle.WaitOne() and never call Set().

Is there a better way of doing this?

Thanks for the help in advance.

UPDATE:

This is an overview of the application.

I need to start independent processes (some exe) on demand, containing wcf service. WCF service should listen idefinetly and that is why I need this functionality. The most similar approach I can find is IIS (many w3wp processes running at the same time).

w3svc.exe (IIS windows service) starts many instances of w3wp.exe depending on the number of configured app pools and the requests, it receives.

In my application I want to keep up the processes representing w3wp.exe in the IIS infrastructure, not w3svc. What is the kind of message loop that would keep alive w3wp in .NET?

Upvotes: 3

Views: 5232

Answers (5)

Jeremy
Jeremy

Reputation: 58

System.Threading.Thread.Sleep(millisenconds);

Upvotes: 0

Servy
Servy

Reputation: 203802

In your update you say that the program is starting several other programs using Process. (It happens to be 'yourself' but that doesn't matter.)

If the program has already done that it doesn't sound like it has any more to do. That process ending won't kill all of the processes it spawned.

You can use the process.WaitForExit() to wait for the processes that you spawn to all exit, rather than just spinning doing nothing, if for some reason you really need to keep the process alive. If there is something that it actually needs to do after spawning the other processes then you'd need to tell us what that is, because if there is something you should be waiting on an event of some sort, which is something you haven't brought up.

Edit: you claim that all the process is doing is "listening". Define that task. If you have a blocking GetNextRequest method then you simply have: while(true){GetNextRequest();}. If it's non blocking, then use use a BlockingCollection<MyRequests> in which the receive method/event hanlder adds a new item to the collection and the main thread had a while loop just like I mentioned before reading from the blocking collection. The point is that you shouldn't ever just sit there and do nothing; you process is doing something, so do that in a while(!done) or while(true) loop. If the method isn't blocking, it's a reasonably well defined problem to solve; just wrap it in a blocking method call.

Upvotes: 0

Timmerz
Timmerz

Reputation: 6199

IIS is a windows service which is why it runs like this. You might look at other options like a single service where you can invoke it via an api and tell it to start another thread or listener. Starting new instances of applications isn't the best option. Typically windows applications have a messagepump, which is a while loop I think...which would prevent it from exiting.

However, you can also follow the example here, which I believe does not close the formless window:

Run Formless Notification User Control?

Upvotes: 1

YoryeNathan
YoryeNathan

Reputation: 14502

You can do that in sooo many ways. I personally like this one, as it is very readable and self explanatory:

Process.GetCurrentProcess().WaitForExit();

Upvotes: 8

Nikola Anusev
Nikola Anusev

Reputation: 7078

while(true)
{
    // to make it less CPU intensive
    Thread.Sleep(1000);
}

Of course, any solution you can think of will not prevent the forceful termination of application by killing its process.

Upvotes: 1

Related Questions