79492369

Date: 2025-03-07 12:58:08
Score: 1.5
Natty:
Report link

I'm unable to comment but would like to share an opinion:

Task.Factory.StartNew is queueing the callback on to the ThreadPool. This can be seen if one looks at the source code:

Which means that the Console.ReadKey is running in the background thread. This is interesting because the Task will not be started immediately, which gives time for other stuff to happen in the mean time (unlike the direct approach).

The main difference between these two is that the former one will continue executing while the latter will block until a key is read (in both examples the Task is not awaited).

Calling the ReadConsoleInput directly is what is causing the issue, here's why I think that: somewhere before ReadConsoleInput is called you are calling some other un-awaited async Console method which does some kind of reading (Console.In.[...], Console.Out.[...] etc.) or something else Console related.

Both Console.ReadKey and the other method are in a deadlock because both are locking on themselves:

Reason why starting the process with TaskFactory is working is because the other method completed its work or got disposed.

I will delete this answer if any of the above is wrong (assumptions or otherwise) or unhelpful.

Reasons:
  • Blacklisted phrase (1): to comment
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: bgajic