0

I'm new to multi-threaded C#, so this is probably a pretty simple/basic question. I have a console app that uses a single worker thread to execute a task. When the worker task finishes, i want to exit the console app with an int. I'm confused about how to do this, since the Main() method requires a return value, but the thread executes asynchronously. Here's what I'm trying to do:

class Program
{

    static void Main(string[] args)
    {
        var exitCode = 1;

        var deploymentManager = new DeploymentManager(() =>
                {
                    exitCode = 0;
                    return exitCode; //<----- doesn't work because main() needs to return something immediately
                });

        var workerThread = new Thread(deploymentManager.Deploy);

        workerThread.Start();
    }
}  

This illustrates, how I want to return the exitcode value, only when the thread has finished and executes the callback action.

Tyler Jones
  • 1,283
  • 4
  • 18
  • 38
  • 2
    Just do the work in the main thread. What's the point in starting another thread only to then let the main thread die? – Servy Sep 02 '14 at 14:43
  • What's your question? `Main` is allowed to return an int value – Jonesopolis Sep 02 '14 at 14:46
  • @Jonesy That doesn't solve his problem. The `Main` thread is completed before he has computed the exit code. – Servy Sep 02 '14 at 14:47
  • I understand that. Just wait on the thread to finish. 2 minutes of research would resolve all of this. – Jonesopolis Sep 02 '14 at 14:48
  • @Jonesy, when you say "wait on the thread to finish", that's kind of what i'm asking how to do. how do i wait on the thread to finish, and how do i then return a value that has been updated asynchronously? – Tyler Jones Sep 02 '14 at 14:57
  • [look here](http://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net) – Jonesopolis Sep 02 '14 at 14:59

1 Answers1

2

You can use Enviornment.Exit to specify an exit code for the application from some location other than the Main method:

static void Main(string[] args)
{
    var exitCode = 1;

    var deploymentManager = new DeploymentManager(() =>
            {
                exitCode = 0;
                Environment.Exit(exitCode);
            });

    var workerThread = new Thread(deploymentManager.Deploy);

    workerThread.Start();
}

That said, there's really no reason to create a new thread here if you're just going to let the main thread die. You may as well just do the work right there. This would really only be useful if the main thread is going to be doing other work while the other thread is executing.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • The reason for the thread, is the work in the background task causes the app to freeze. I need to it remain responsive during the work, so I have do that work in a worker thread. – Tyler Jones Sep 02 '14 at 14:54
  • @TylerJones If this is a winform application and the main thread is pumping a message loop then you should say that in the question. Winforms has a framework in place specifically to addressing some of these types of concerns. Although it also brings up the question of why you're worried about the return code of a UI application. – Servy Sep 02 '14 at 14:57
  • it's a console application. Not a winforms application. – Tyler Jones Sep 02 '14 at 14:58
  • 2
    Then what is freezing? What is or is not being responsive? The console is going to be equally responsive with or without another thread. – Servy Sep 02 '14 at 14:58