my program has two threads, grab data from two cameras, each of thread will be used to update a PictureBox:
private void StartGrabLoop()
{
m_grabThread = new BackgroundWorker();
m_grabThread.ProgressChanged += new ProgressChangedEventHandler(UpdateUI);
m_grabThread.DoWork += new DoWorkEventHandler(GrabLoop);
m_grabThread.WorkerReportsProgress = true;
m_grabThread.RunWorkerAsync();
}
private void StartGrabLoop1()
{
m_grabThread1 = new BackgroundWorker();
m_grabThread1.ProgressChanged += new ProgressChangedEventHandler(UpdateUI1);
m_grabThread1.DoWork += new DoWorkEventHandler(GrabLoop1);
m_grabThread1.WorkerReportsProgress = true;
m_grabThread1.RunWorkerAsync();
}
private void GrabLoop(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (m_grabImages)
{
try
{
m_camera.RetrieveBuffer(m_rawImage);
}
catch (FC2Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
continue;
}
lock (this)
{
m_rawImage.Convert(PixelFormat.PixelFormatBgr, m_processedImage);
}
worker.ReportProgress(0);
}
m_grabThreadExited.Set();
}
private void GrabLoop1(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (m_grabImages1)
{
try
{
m_camera1.RetrieveBuffer(m_rawImage1);
}
catch (FC2Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
continue;
}
lock (this)
{
m_rawImage1.Convert(PixelFormat.PixelFormatBgr, m_processedImage1);
}
worker.ReportProgress(0);
}
m_grabThreadExited1.Set();
}
private void UpdateUI(object sender, ProgressChangedEventArgs e)
{
Bitmap source = m_processedImage.bitmap;
Bitmap resized = new Bitmap(source, source.Width / zoom, source.Height / zoom);
pictureBox1.Image = resized;
pictureBox1.Invalidate();
}
private void UpdateUI1(object sender, ProgressChangedEventArgs e)
{
Bitmap source = m_processedImage.bitmap;
Bitmap resized = new Bitmap(source, source.Width / zoom, source.Height / zoom);
pictureBox2.Image = resized;
pictureBox2.Invalidate();
}
The trick thing is it could run very well on my desktop. but have memory problem on my laptop, which either show white image, or busy to crash. I did lots of research and tried dispose(),or System.GC.Collect(); System.GC.WaitForPendingFinalizers();none of them work, anybody could help me? The reason I use "new bitmap" is because I want to simple zoom function....