Search code examples
c#zxingaforge

Why is System.Windows.Forms.Timer stopping automatically when window is fulscreen or is large?


Following online samples, I have written qrcode web cam reader in c# uzing zXing and aForge libraries. I have encountered very weird behavior of System.Windows.Forms.Timerin C#: I have dropped it on widows form, enabled it, set interval 1 second and attached tick eventhandler.

Everything seems to work normally, but when i resize(enlarge) the window to particular size, or if i make window fullscreen, the times tick event stops firing. When i bring window from fullscreen to normal size, or when i reduce window size, the timer starts again by itself automatically.

I am using following version of visual studio:

enter image description here

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using AForge.Video;
using AForge.Video.DirectShow;

using ZXing;

namespace QrCodeWebCamReader
{
    public partial class Form1 : Form
    {
         
        FilterInfoCollection filterInfoColletion;
        VideoCaptureDevice captureDevice;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        { 
            filterInfoColletion = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach(FilterInfo filterInfo in filterInfoColletion)
            {
                cboDevice.Items.Add(filterInfo.Name);
            }
            cboDevice.SelectedIndex = 0;
        }

        private void BtnStart_Click(object sender, EventArgs e)
        {
            captureDevice = new VideoCaptureDevice(filterInfoColletion[cboDevice.SelectedIndex].MonikerString);
            captureDevice.NewFrame += CaptureDevice_NewFrame;
            captureDevice.Start();
            timer1.Start();
        }

        private void CaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {

            // pictureBox.Image = (Bitmap)eventArgs.Frame.Clone();

            Bitmap img = (Bitmap)eventArgs.Frame.Clone(); 

            using (Graphics gr = Graphics.FromImage(img))
            {
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

                Rectangle cropArea = new Rectangle(img.Width / 2 - 150, img.Height / 2 - 150, 300, 300);

                gr.DrawRectangle(new Pen(Color.Red,5), cropArea); 
            }


            pictureBox.Image = (Bitmap)img.Clone();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            if (captureDevice.IsRunning)
            {
                captureDevice.Stop();
            }
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("fungus");
            System.Diagnostics.Debug.WriteLine(pictureBox.Image);

            if(pictureBox.Image != null)
            {
                Rectangle cropArea = new Rectangle(pictureBox.Image.Width / 2 - 150, pictureBox.Image.Height / 2 - 150, 300, 300);
                BarcodeReader barcodeReader = new BarcodeReader();
                Result result = barcodeReader.Decode((Bitmap)((Bitmap)pictureBox.Image).Clone(cropArea, pictureBox.Image.PixelFormat));
                picCropped.Image = (Bitmap)((Bitmap)pictureBox.Image).Clone(cropArea, pictureBox.Image.PixelFormat);
                if (result != null)
                {
                    txtQRCode.Text += result.ToString();
                    Console.Beep();
                }
            } 
        }
    }
}

My project is configured to work with net framework 2.0

What could be the cause of this behavior and how can i prevent it from happening?

Thank you

EDIT: The only logic I can think of regarding this behavior is that maybe compiler is doing some sort of optimization / obfuscation / minimization of the generated executable code? How is it possible to turn of optimization /obfuscation / minimization in visual studio community version for c# windows forms application?

UPDATE: This behavior happens only if video is being captured. If video is not being captured, timer is not stopping.


Solution

  • After Extensive research, i Have replaced System.Windows.Forms.Timer with System.Timers.Timer And everything started to worked normally and errors went away.

    My setup for System.Timers.Timer looks Like This:

            System.Timers.Timer timer1;
      
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1 = new System.Timers.Timer(600);
                timer1.Enabled = true;
                timer1.SynchronizingObject = this;
                timer1.Elapsed += Timer1_Elapsed;
                timer1.Start();
     
            }
    

    But, i still have no answer what was the reason of the original error. Any info regarding this issue would be great.