Performance difference between Synchronous HTTP Handler and Asynchronous HTTP Handler

后端 未结 2 1671
失恋的感觉
失恋的感觉 2020-12-14 07:28

Is there a performance difference between Synchronous HTTP Handler and Asynchronous HTTP Handler? IHttpHandler vs IHttpAsyncHandler

Why choose one over another?

2条回答
  •  -上瘾入骨i
    2020-12-14 07:59

    There's no performance difference aside from managing another thread.

    Synchronous is easier to code. You send the request and the thread freezes until the response is returned. Then you can handle the response and errors in the same method. It's easy to read and debug. If you run this code in your GUI thread, Windows may report that your program is "not responding" if you don't receive a response quickly.

    Use Asynchronous if you don't want your thread to freeze. The user can continue to interact with the program while a background task waits for the HTTP response. Then you have to write code to manage the background task, watch when it is complete, handle errors, pass these errors back to the GUI thread, etc. It's a bit more work, and a lot harder to read and debug, but ultimately a better quality product if it's done properly.

    Edit: Corrected that synchronous methods freeze the thread, not necessarily the whole program.

提交回复
热议问题