Using Httprequest to get pictures from given URL

会有一股神秘感。 提交于 2019-12-04 21:59:21
arcain

The code to fetch the images is synchronous. I would suggest modifying your code to use HttpWebRequest.BeginGetResponse() to fetch your images as detailed in this answer.

So when your (single) timer fires, begin the async requests to your cams, then (as they complete) the callback will fire for each completed request, and you can save off the returned image.

If you need to optimize further, then you could delegate saving the image to disk to a background ThreadPool work item.

Any time you need to get responses back faster than stacked responses allow, you end up having to complete multiple pieces of work at once, using more than one thread (last word is the clue here).

In .NET, this means either setting up your own threading library, using TPL, etc.

ADDED: This could mean, as pointed out by a comment below, using asynchronous calls and allowing the framework to implicitly create threads for you, instead of controlling them yourself. In fact, you could use thread pooling and still choose asynch or synch without controlling the number of threads created. If you really want to get down to the metal, you can even offload threading to the OS using an interrupt handler at the OS level (a menthod used by Ruby, for example, prior to 1.9, which introduced "true" threading rather than "green" threads - yeah, TMI).

The answer still stands if you have 2 minutes worth of work to get done and 30 seconds to get it done in, you need a multi-threaded solution, whether you are aware you are actually threading (explicit creation) or not (implicit creation handled by framework/OS).

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