Question

Suppose I have a .net console application that executes some long-running code on a thread.

I would like to make the console application terminate while the thread is still running. Specifically, I would like to launch the console application from a batch file or other process that will kick off the long-running process and then continue doing other things while the long-running thread is executing.

If it isn't possible, the only workaround I know would be to have a service program host the long-running thread.

Is it possible to make a console application return control like I described?

Was it helpful?

Solution

Threads run in the context of the process that hosts them. So if you terminate your console or "return control", which you can do, your thread won't complete (more likely your thread will prevent your application from exiting, which is also what you don't want).

You can simulate returning control to the batch file/shell with the start command:

c:\>start myconsole

This creates a new command window in which your process runs synchronously, but the batch file from whence you launched it won't have to wait.

You can also "simulate" it by spawning a child process. See Process.Start() for an idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top