user1615362 user1615362 - 10 days ago 5
C# Question

How to pause task execution

I have this code that creates a task:

Task.Factory.StartNew(() =>
{
ExtractStuff(fileName);
});


Sometimes I need to pause for few seconds inside
ExtractStuff


Is it OK to use the regular
Thread.Sleep(1000)
? Or is there another way to pause the running task?

Answer

I believe it is fine to do this but preferable to use TaskCreationOptions.LongRunning.

Task.Factory.StartNew(() =>
                    {
                        ExtractStuff(fileName);
                    },TaskCreationOptions.LongRunning);