-2

I'm working on some old code that runs on custom background thread. this thread uses the thread class and passes it a thread delegate i.e. an action.

_thread = new Thread(() => processData());

i need to call some newer functions that return task so do i just do

myfunc().GetAwaiter().GetResult();

or is there some other way? because since it is a custom thread, i don't think if it is really doing anything this time. how can i call async await from. the custom thread so that it is utilized properly?

Simple Fellow
  • 4,315
  • 2
  • 31
  • 44

1 Answers1

0

If you want to continue using your custom Thread, then yes, it has to block on asynchronous code. GetAwaiter().GetResult() is probably your best bet for that.

or is there some other way?

Yes. If you can replace your custom Thread with a thread pool thread, then you can use Task.Run instead of Thread, and then you can use the more natural await instead of GetAwaiter().GetResult(). Most custom threads can be replaced by thread pool threads, but this is not always the case.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810