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:
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());
}
}
}