Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Thread.Sleep with Task.Run without blocking main tread

Is it possible to make sure that a task runs on another thread than the main thread? So that this piece of code would not bock the calling thread?

var task = Task.Run(() =>
{
    //third party code i don't have access to
    Thread.Sleep(3000);
});

I know that there is Task.Delay but I want to make sure that the task will run on another thread.

like image 764
thalm Avatar asked Jan 09 '17 19:01

thalm


2 Answers

I think that there are different ways to accomplish what you are trying. But based on what you are trying to do, I think that you would be fine just using async/await which is not going to block your UI for sure, and will allow you to control your task asynchronously.

As MSDN says:

Async methods are intended to be non-blocking operations. An await expression in an async method doesn’t block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method. The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available.

https://msdn.microsoft.com/en-us/library/mt674882.aspx

This is an example of usage:

public async Task MyMethodAsync()
{
    Task<int> runTask = RunOperationAsync();

    int result = await longRunningTask;

    Console.WriteLine(result);
}

public async Task<int> RunOperationAsync()
{
    await Task.Delay(1000); //1 seconds delay
    return 1;
}

This won´t block your UI

like image 126
NicoRiff Avatar answered Nov 09 '22 16:11

NicoRiff


@Nico is correct in that ideally, you could use asynchronous programming all the way. That approach is ideal because it doesn't waste a thread sleeping (or, in a more realistic example, blocking on I/O).

Is it possible to make sure that a task runs on another thread than the main thread?

Yes. Task.Run will always queue work to the thread pool, and the main (entry point) thread is never a thread pool thread.

like image 2
Stephen Cleary Avatar answered Nov 09 '22 15:11

Stephen Cleary