I understand this question may be too general. But I tried many things and I am not able to figure out how to resolve this.
I am using ConcurrentQueue for multithreading operation. One thread is downloading Images from server and saving it to queue. Here is the code for that:
public static void DownloadImage()
{
string baseUrl = "http://someurl";
//int numIterations = 5;
HttpWebRequest request = null;
foreach (var fileName in fileNames)
{
string url = string.Format(baseUrl, fileName);
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
var response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
img = Image.FromStream(stream);
ImageFileName FileNameImage = new ImageFileName(fileName, img);
ImageQueue.Enqueue(FileNameImage);
Console.WriteLine("Count after Enqueue: {0}", ImageQueue.Count);
}
And another thread takes images from Queue and saves them on destination folder. Here is the code for that:
public static void SaveImage()
{
while (true)
{
if (!ImageQueue.IsEmpty)
{
foreach (var newobject2 in ImageQueue)
{
Image img2 = newobject2.Image;
img2.Save("C:\\path" + newobject2.ImageName);
ZoomThumbnail = img2;
ZoomSmall = img2;
ZoomLarge = img2;
ZoomThumbnail = GenerateThumbnail(ZoomThumbnail, 86, false);
ZoomSmall = GenerateThumbnail(ZoomSmall, 400, false);
ZoomLarge = GenerateThumbnail(ZoomLarge, 1200, false);
ZoomThumbnail.Save("C:\\path" + newobject2.ImageName + "_Thumb.jpg");
ZoomSmall.Save("C:\\path" + newobject2.ImageName + "_ZoomSmall.jpg");
ZoomLarge.Save("C:\\path" + newobject2.ImageName + "_ZoomLarge.jpg");
ImageFileName imgobject3 = new ImageFileName();
ImageQueue.TryDequeue(out imgobject3);
Console.WriteLine("Count after Deque: {0}", ImageQueue.Count);
}
}
}
}
I am calling these two threads from Button_Click() like this:
Thread DownloadThread = new Thread(DownloadImage);
DownloadThread.Start();
Thread SaveThread = new Thread(SaveImage);
SaveThread.Start();
I am getting MemoryFull error whenever queue reaches count of 68. I am not sure how I can avoid that. I have tried using Thread.Sleep to avoid this. For Example, I tried: Thread.Sleep(500)
after foreach
loop. Whenever I try it inside foreach
it works totally fine as at any given point ImageQueue.Count = 1
. Where I am getting wrong?