I encountered a simple problem when trying to test TPL. I would like to get numbers (from 1 to 1000) for example in the console window.
This is the code that I have:
class Program
{
static void Main(string[] args)
{
Task.Factory.StartNew(Calculate).ContinueWith(task =>
{
Task.WaitAll();
Console.ReadKey();
});
}
private static void Calculate()
{
for (var number = 0; number < 1000; number++)
{
Console.WriteLine(number);
}
}
}
The problem is that window just closes and it doesn't show anything. I know that I messed something up in ContinueWith
method. I suspect that ContinueWith
is not executed (because window just closes without my input) but I don't know why.
Any suggestions are helpful (Tried reading MSDN but to no avail). Thanks in advance.
StartNew
create a new thread? because your main thread will just continue and will exit the application. shouldn't something be coded to at least hold the main thread until your background stuff is done? – Expediency