简体   繁体   中英

Using method DownloadString() or another one?

I'm making a C# program in my Visual Studio and I have button that on "Click" is suppose to make request to .php file in Internet and to read json, rest or soap (not sure which is the most confortable for C#). I'm using new WebClient().DownloadString(url);
My questions are
- why the first time I click the button (use the function) the whole program freeze and after that It's not freezing ?
- how to prevent the "freezing effect" ?
- is there a better way to retrieve that information from the web ?
- json, rest or soap is preferable?

  • why the first time I use the function the whole program freeze ?

You're connecting to an external resource, this will take time, and you're consuming content from this resource which also takes time. DownloadString is a blocking task your main application will wait until it has returned something.

  • how to prevent the "freezing effect" ?

Use async methods that WebClient provides. DownloadStringTaskAsync .

Documentation can be found here: https://msdn.microsoft.com/en-us/library/hh194294%28v=vs.110%29.aspx

Previous asked question: Webclient calls to DownloadString cause my app to freeze

That's all I have for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM