简体   繁体   中英

How to properly dispose of resources in wpf

Hi I have an application in which I have to save images from three different IP Cameras whenever a button is pressed.

I am using a class that has all the members that I need to save the images from the IP camera namely the BitmapImage and the DateTime of when the photo was saved.

I have the following problem. I need to save a certain amount of photos of each camera every couple hundred milliseconds. And I am currently testing it by saving 50 photos of each camera every 200ms to a ConcurrentQueue and then the items gets saved from the ConcurrentQueue to file. After I have taken about 110 photos altogether of all three cameras then it just saves blank images.

I think my problem is that the program memory is too full, so I need to clear an item from the memory when ever I save the item with the TryDequeue() method of the ConcurrentQueue.

Can anyone please advise me or give me maybe some links that can help me to save this problem so that I can save as many photos as I want to of each camera and that it will not run out of memory after a certain amount photos?

A button is pressed and then it goes into a for loop where it calls the following method.

private void EnqueuePhotos1()
    {
        IPCamera1 ipCam1Enqueue = new IPCamera1();
        BitmapImage cam1Image = new BitmapImage();
        cam1Image.BeginInit();
        cam1Image.CacheOption = BitmapCacheOption.OnLoad;
        cam1Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        cam1Image.UriSource = null;
        cam1Image.UriSource = new Uri("http://" + ipCam1IP + "/image?res=full&x0=0&y0=0&x1=1600&y1=1200&quality=21&doublescan=0", UriKind.Absolute);
        while (cam1Image.IsDownloading) { ; }
        cam1Image.EndInit();
        ipCam1Enqueue.IPCamImage = cam1Image;
        ipCam1Enqueue.TimeTook = DateTime.Now;
        ipCam1ConQ.Enqueue(ipCam1Enqueue);
    }

for a certain amount of times depending on how many photos the user wants to take.

Just before the for loop I start my timer to check every 100ms if there is something on the ConcurrentQueue and then if something is found it calls the following function.

private void GetPhotos1()
    {
        IPCamera1 ipCam1Dequeue = new IPCamera1();
        while (ipCam1ConQ.TryDequeue(out ipCam1Dequeue))
        {
            cam1Photos++;
            cam1ImgLoc = cam1Location + "\\Image " + cam1Photos + ".jpg";
            FileStream cam1Stream = new FileStream(cam1ImgLoc, FileMode.Create);
            JpegBitmapEncoder cam1Encoder = new JpegBitmapEncoder();
            cam1Encoder.Frames.Add(BitmapFrame.Create(ipCam1Dequeue.IPCamImage));
            cam1Encoder.Save(cam1Stream);
            cam1Stream.Dispose();
        }
    }
using (FileStream cam1Stream = new FileStream(cam1ImgLoc, FileMode.Create))
{
    // do stuff...
}

Resources defined in a way like this are automagically disposed after the statements in the using statement are executed.

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