Should I provide Sync or Async methods for developers in my IMDB API library?

孤街浪徒 提交于 2019-12-13 07:25:51

问题


I'm going to make a DLL Library that'll alow developers to search and access IMDB movie pages.

How should I handle the freezing in the GUI, should I use async methods, or should I allow a dev to manually create a thread for using our DLL?


回答1:


This is entirely up to you.

That being said, I think providing an asynchronous and a synchronous API will make your library act more like the framework libraries, which will give your users the best chance of using it correctly.

For example, WebClient provides DownloadFile and DownloadFileAsync. I would, personally, mimic this behavior in my API. If you implement your library asynchronously, it's very easy to wrap this in a synchronous API.




回答2:


This is a judgement call on your end. Consider, however:

  1. The user can always use a BackgroundWorker to avoid blocking, if you only expose in a synchronous way.

  2. If you feel that you can provide some intermediary data during the action (like progress percentage) then an asynchronous version may be useful.




回答3:


I would avoid asynchronous calls within a library like this.

What if the user of the library doesn't care about blocking? What if it's a command-line script that should block?

The threading of an application is a major design decision, avoid making for them.

I would first implement the blocking, synchronous calls and perhaps later, add asynchronous methods to make the library easier to use for thread-shy programmers.



来源:https://stackoverflow.com/questions/1663871/should-i-provide-sync-or-async-methods-for-developers-in-my-imdb-api-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!