Question

I wonder, how can I run the method Main() of C# program in the thread pool like I do it in F#:

let main() =
    Console.WriteLine("Hello World!")

do Task.Factory.StartNew(main).Wait()

What is the alternative code in C# programming language? Because in the simple console application, there is only the way to use:

static void Main() // or with int as the return type and args[]
                   // in method arguments but it's not important for this question

And I can't somewhere also add smth like in F#:

do Task.Factory.StartNew(main).Wait()

Or I just don't know the all possibilites of C# program execution?

Était-ce utile?

La solution

Could I suggest that you refrain from that. I'm fairly certain that you should write other functions which you can then add to the thread pool as required.

In most programming languages, it's bad style to call the main function; write other functions to do your work if that's how you want to do it. Basically:

static void Function(){
    //Do stuff
}
static void Main(){
  TaskFactory.StartNew(Function).wait();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top