0

Currently doing a Motion sensor detected project and this error appear sometimes not everytime. The motion sensor wil detect motion, then once the sensor detects motion, the webcam will get status from firebase database then activate the webcam and automatically capture image and saved as JPG, plays a voice message notifying user, sends a Email to user. Then at the same time the picture box will start streaming for users. Could it be I added to many functions at once? But I need this function to fulfil my Final Year Project objectives.

This is the Call stack:

Call Stack

And the exception Exception

Not sure how to fix this and I need to get help from here. Need this to pass my last semester.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FireSharp.Config;
using FireSharp.Interfaces;
using FireSharp.Response;
using AForge.Video;
using AForge.Video.DirectShow;
using Tulpep.NotificationWindow;
using System.Media;
using System.Net;
using System.Net.Mail;
using S22.Imap;

namespace securitycam
{
    public partial class Form1 : Form
    {
        SoundPlayer splayer1 = new SoundPlayer(@"E:\FYP 2 edit 28April\testing.mp3");

        IFirebaseConfig config = new FirebaseConfig
        {
            AuthSecret = "OCO7eR5nshmvna3nXez7Eoodf3LseRlIal9RrieX",                
            BasePath = "https://securitycam-7c2e1-default-rtdb.firebaseio.com/"     
        };
        IFirebaseClient client;
        FilterInfoCollection filterInfoCollection;
        VideoCaptureDevice videoCaptureDevice, videoCaptureDevice2, videoCaptureDevice3;
        int trig1 = 0, trig2 = 0, trig3 = 0, cnt = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            client = new FireSharp.FirebaseClient(config);
            if (client != null)
            {
                MessageBox.Show("Welcome to PIR Monitoring!");
            }
            filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);            sources
            foreach (FilterInfo filterInfo in filterInfoCollection)
            {
                comboBox1.Items.Add(filterInfo.Name);
                comboBox2.Items.Add(filterInfo.Name);
                comboBox3.Items.Add(filterInfo.Name);
            }
            comboBox3.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox1.SelectedIndex = 0;
            videoCaptureDevice = new VideoCaptureDevice();
            videoCaptureDevice2 = new VideoCaptureDevice();
            videoCaptureDevice3 = new VideoCaptureDevice();
        }



        private void button4_Click(object sender, EventArgs e)
        {
            videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonikerString);
            videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
            videoCaptureDevice.Start();                                 


        }
        private void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
         {
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
            if (trig1 == 1)
            {
                
                pictureBox1.Image.Save(textBox1.Text + "cam1.jpeg");        
                trig1 = 2;

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            videoCaptureDevice2 = new VideoCaptureDevice(filterInfoCollection[comboBox2.SelectedIndex].MonikerString);
            videoCaptureDevice2.NewFrame += VideoCaptureDevice_NewFrame2;
            videoCaptureDevice2.Start();                                

        }

        private void button6_Click(object sender, EventArgs e)
        {
            videoCaptureDevice3 = new VideoCaptureDevice(filterInfoCollection[comboBox3.SelectedIndex].MonikerString);
            videoCaptureDevice3.NewFrame += VideoCaptureDevice_NewFrame3;
            videoCaptureDevice3.Start();                                

        }

        private void button7_Click(object sender, EventArgs e)
        {
            

            videoCaptureDevice.SignalToStop();
            videoCaptureDevice.WaitForStop();
            videoCaptureDevice.Stop();                  
            pictureBox1.Image = null;                   
            pictureBox1.Invalidate();

        }

        private void button8_Click(object sender, EventArgs e)
        {
            
            videoCaptureDevice2.SignalToStop();
            videoCaptureDevice2.WaitForStop();
            videoCaptureDevice2.Stop();                 
            pictureBox2.Image = null;                   
            pictureBox2.Invalidate();

        }

        private void button9_Click(object sender, EventArgs e)
        {
            
            videoCaptureDevice3.SignalToStop();
            videoCaptureDevice3.WaitForStop();
            videoCaptureDevice3.Stop();             
            pictureBox3.Image = null;               
                
                sendMail1();


            }
            if (obj.sens02 == "X" && trig2 == 0)
            {
                videoCaptureDevice2 = new VideoCaptureDevice(filterInfoCollection[comboBox2.SelectedIndex].MonikerString);
                videoCaptureDevice2.NewFrame += VideoCaptureDevice_NewFrame2;
                videoCaptureDevice2.Start();
                
                trig2 = 1;
                
                SoundPlayer splayer2 = new SoundPlayer(@"E:\FYP 2 edit 28April\cam 2.wav");
                splayer2.Play();
                sendMail2();


            }
            if (obj.sens03 == "X" && trig3 == 0)
            {
                videoCaptureDevice3 = new VideoCaptureDevice(filterInfoCollection[comboBox3.SelectedIndex].MonikerString);
                videoCaptureDevice3.NewFrame += VideoCaptureDevice_NewFrame3;
                videoCaptureDevice3.Start();
                
                trig3 = 1;
                
                SoundPlayer splayer3 = new SoundPlayer(@"E:\FYP 2 edit 28April\cam 3.wav");
                splayer3.Play();
                sendMail3();

            }

        }

        private void VideoCaptureDevice_NewFrame3(object sender, NewFrameEventArgs eventArgs)
        {
            pictureBox3.Image = (Bitmap)eventArgs.Frame.Clone();
            if (trig3 == 1)
            {
                pictureBox3.Image.Save(textBox1.Text + "cam3.jpeg");        
                trig3 = 2;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoCaptureDevice.IsRunning == true)
                videoCaptureDevice.Stop();
            videoCaptureDevice2.Stop();
            videoCaptureDevice3.Stop();             
            trig1 = trig2 = trig3 = 1;
        }

        public void Alert(string msg)
        {
            Alert_cam1 frm = new Alert_cam1();
            frm.showAlert(msg);
        }


        private void button16_Click(object sender, EventArgs e)
        {
            var message = new MailMessage(txtEmail.Text, txtRecipient.Text);
            message.Subject = txtSubject.Text;
            message.Body = rtxtBody.Text;

            using (SmtpClient mailer = new SmtpClient("smtp.gmail.com", 587))
            {
                mailer.Credentials = new NetworkCredential(txtEmail.Text, txtPassword.Text);
                mailer.EnableSsl = true;
                mailer.Send(message);
            }
            
        }

        public void sendMail1()
        {
            var message = new MailMessage(txtEmail.Text, txtRecipient.Text);
            message.Subject = txtSubject.Text;
            message.Body = rtxtBody.Text;

            using (SmtpClient mailer = new SmtpClient("smtp.gmail.com", 587))
            {
                mailer.Credentials = new NetworkCredential(txtEmail.Text, txtPassword.Text);
                mailer.EnableSsl = true;
                mailer.Send(message);
            }
        }

        public void sendMail2()
        {
            var message = new MailMessage(txtEmail2.Text, txtRecipient2.Text);
            message.Subject = txtSubject2.Text;
            message.Body = rtxtBody2.Text;

            using (SmtpClient mailer = new SmtpClient("smtp.gmail.com", 587))
            {
                mailer.Credentials = new NetworkCredential(txtEmail2.Text, txtPassword2.Text);
                mailer.EnableSsl = true;
                mailer.Send(message);
            }
        }

        public void sendMail3()
        {
            var message = new MailMessage(txtEmail2.Text, txtRecipient3.Text);
            message.Subject = txtSubject3.Text;
            message.Body = rtxtBody3.Text;

            using (SmtpClient mailer = new SmtpClient("smtp.gmail.com", 587))
            {
                mailer.Credentials = new NetworkCredential(txtEmail3.Text, txtPassword3.Text);
                mailer.EnableSsl = true;
                mailer.Send(message);
            }
        }


    }
}

The main function I think occurring this error is this part...

private async void interval(object sender, EventArgs e)
{
    FirebaseResponse response = await client.GetTaskAsync("monitoring");
    Data1 obj = response.ResultAs<Data1>();

    if (obj.sens01 == "X" && trig1 == 0)
    {
        videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonikerString);
        videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
        videoCaptureDevice.Start();
        trig1 = 1;

        SoundPlayer splayer1 = new SoundPlayer(@"E:\FYP 2 edit 28April\cam 1.wav");
        splayer1.Play();
        sendMail1();


    }
    if (obj.sens02 == "X" && trig2 == 0)
    {
        videoCaptureDevice2 = new VideoCaptureDevice(filterInfoCollection[comboBox2.SelectedIndex].MonikerString);
        videoCaptureDevice2.NewFrame += VideoCaptureDevice_NewFrame2;
        videoCaptureDevice2.Start();
        trig2 = 1;
        SoundPlayer splayer2 = new SoundPlayer(@"E:\FYP 2 edit 28April\cam 2.wav");
        splayer2.Play();
        sendMail2();


    }
    if (obj.sens03 == "X" && trig3 == 0)
    {
        videoCaptureDevice3 = new VideoCaptureDevice(filterInfoCollection[comboBox3.SelectedIndex].MonikerString);
        videoCaptureDevice3.NewFrame += VideoCaptureDevice_NewFrame3;
        videoCaptureDevice3.Start();
        trig3 = 1;
        SoundPlayer splayer3 = new SoundPlayer(@"E:\FYP 2 edit 28April\cam 3.wav");
        splayer3.Play();
        sendMail3();

    }
}

This is the code that is showing everytime the software breaks and shows the error of the title I published..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace securitycam
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
           
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            
            
        }
    }
}
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
  • Check the event viewer to see if any error are occurring when app fails. – jdweng Apr 30 '21 at 15:13
  • Please add the stack trace or can you mark the line where the exception happens? Also, please remove commented code if it isn't helping the question. – Fildor Apr 30 '21 at 15:15
  • Ok, now. A lot of your functions have side-effects. You need to account for them. In your interval, if you get a trigger, you start recording, regardless of the current state of the device. You need to check that first, to make sure, you actually _can_ start - or need to start again for that matter. – Fildor Apr 30 '21 at 15:22
  • The other thing - which may contribute but not be the direct source of your problem - is the complete mess this class is. But if you are already short on time, you may not be able to make this "pretty"... just make it work. – Fildor Apr 30 '21 at 15:24
  • Hi guys thanks for the quick comment... I just updated the post... deleted the comments and added the exception when the software breaks. There's a thing called "Lock" that i can use right? but im not familiar with it can that help? – Chee Ken Lam Apr 30 '21 at 15:29
  • No, locking won't help here. You are just going to introduce dead-locks then. Which will be worse than "just" blocking the event handling thread which you are doing now. – Fildor Apr 30 '21 at 15:31
  • And yea im short on time apparently haha... So i need a quick fix for this error – Chee Ken Lam Apr 30 '21 at 15:31
  • owh so lock wont help. Can I get any solutions from you sir? – Chee Ken Lam Apr 30 '21 at 15:32
  • You didn't add the exception but the main function. Do you know what a "Stacktrace" is? It's that list that goes with the exception message, like " occurred in somefunction() at line 354 in yaddayadda.cs at ... " – Fildor Apr 30 '21 at 15:35
  • ^^ With this, you can find out which call caused the exception. Then you can refer to the documentation to find out what could be the most probable cause and why and how your program is in a state that matches this cause when it executes the call. Assuming it's somehow related to your camera driver stack, I _guess_ that it doesn't allow to start more than 1 stream at a time. But that's just a shot into the blue. If so, that would mean that you cannot just start a stream. You'd need to make sure the device is not already streaming. – Fildor Apr 30 '21 at 15:37
  • To get to the exact exception data quick, surround the code you suspect to throw the exception with "try/catch", then set a breakpoint in the catchblock and look into the exception object. It should have a "Stacktrace" property. The contents of which you should post here. – Fildor Apr 30 '21 at 15:42
  • added screenshots of exception and call stack.. Is that what i need to look for? – Chee Ken Lam Apr 30 '21 at 15:48
  • First : You have NO exception handler to isolate issue. Second You are not returning any results from your method to indicate when a failure occurs. So once you get an exception you are just continuing executing the rest of the code. third I do not like using statements because you do not get status when an exception occurs. You cannot tell from your code when an exception occurs and then you just continue executing on objects that are not valid. – jdweng May 04 '21 at 10:05

0 Answers0