I'm getting a bit confused with Task.Run
and all I read about it on the internet. So here's my case: I have some function that handles incoming socket data:
public async Task Handle(Client client)
{
while (true)
{
var data = await client.ReadAsync();
await this.ProcessData(client, data);
}
}
but this has a disadvantage that I can only read next request once I've finished processing the last one. So here's a modified version:
public async Task Handle(Client client)
{
while (true)
{
var data = await client.ReadAsync();
Task.Run(async () => {
await this.ProcessData(client, data);
});
}
}
It's a simplified version. For more advanced one I would restrict the maximum amount of parallel requests of course.
Anyway this ProcessData
is mostly IO-bound (doing some calls to dbs, very light processing and sending data back to client
) yet I keep reading that I should use Task.Run
with CPU-bound functions.
Is that a correct usage of Task.Run
for my case? If not what would be an alternative?