Sorry if the title is not clear or correct, dont know what title should i put. Please correct if wrong.
I have this code to download images from IP camera and it can download the images.
The problem is how can i do the images downloading process at the same time for all cameras if i have two or more cameras?
private void GetImage()
{
string IP1 = "example.IPcam1.com:81/snapshot.cgi;
string IP2 = "example.IPcam2.com:81/snapshot.cgi;
.
.
.
string IPn = "example.IPcamn.com:81/snapshot.cgi";
for (int i = 0; i < 10; i++)
{
string ImagePath = Server.MapPath("~\\Videos\\liveRecording2\\") + string.Format("{0}", i, i + 1) + ".jpeg";
string sourceURL = ip;
WebRequest req = (WebRequest)WebRequest.Create(sourceURL);
req.Credentials = new NetworkCredential("user", "password");
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
Bitmap bmp = (Bitmap)Bitmap.FromStream(stream);
bmp.Save(ImagePath);
}
}
There are several methods that will depend on how you want to report feedback to the user. It all comes down to multi-threading.
Here is one example, using the
ThreadPool. Note that this is missing a bunch of error checking throughout… It is here as an example of how to use theThreadPool, not as a robust application:This example simply takes the work you are doing in the for loop and off-loads it to the thread pool for processing. The
DoImageDownloadmethod will return very quickly, as it is not doing much actual work.Depending on your use case, you may need a mechanism to wait for the images to finish downloading from the caller of
DoImageDownload. A common approach would be the use of event callbacks at the end ofBeginDownloadto notify when the download is complete. I have put a simplewhileloop here that will wait until the images finish… Of course, this needs error checking in case images are missing or thedelegatenever returns.Be sure to add your error checking throughout… Hopefully this gives you a place to start.