Async JSON Deserialization

后端 未结 2 417
無奈伤痛
無奈伤痛 2020-12-11 03:38

I need to do a RestRequest and get some JSON, I am not sure if my method is really async since there is still a little freeze in my UI when I use this method.



        
2条回答
  •  独厮守ぢ
    2020-12-11 04:10

    JsonConvert.DeserializeObject is synchronous. You can tell by the fact that it returns you the result of its computation immediately. There is no way it could do something "in the background" and only later hand you the result.

    Move CPU bound work to a thread-pool thread using Task.Run. You can move the whole REST request there if that is more convenient to you.

    Note, that your message box call should run on the UI thread. Better not create a message box on a thread-pool thread like you are doing at the moment. That will result in two UI threads. The message box will not be modal.

提交回复
热议问题