c# - 运动传感器检测到网络摄像头流系统:System.InvalidOperationException:“对象当前正在其他地方使用。”
问题描述
目前正在执行检测到运动传感器的项目,但有时并非每次都会出现此错误。运动传感器会检测到运动,一旦传感器检测到运动,网络摄像头将从firebase数据库获取状态,然后激活网络摄像头并自动捕获图像并保存为JPG,播放语音消息通知用户,向用户发送电子邮件。然后同时图片框将开始为用户流式传输。我可以一次添加到许多功能吗?但我需要这个功能来实现我的最后一年项目目标。
这是调用堆栈:
不知道如何解决这个问题,我需要从这里获得帮助。需要这个来通过我的最后一个学期。
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);
}
}
}
}
我认为发生此错误的主要功能是这部分......
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();
}
}
这是每次软件中断并显示我发布的标题错误时显示的代码。
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());
}
}
}
解决方案
推荐阅读
- reactjs - 访问其他用户的属性 (Cognito)
- c++ - CreateService 失败 0x6f7 存根收到错误数据
- python - 级别名称 b 不是索引的名称
- firebase - 使用firebase登录的谷歌停止工作
- r - R 包“comparedf”的安装失败
- java - 如何获取在另一个类中以编程方式创建的视图 ID?
- sql-server - SQL Server 查询 xml 获取节点
- python - Tensorflow 2.x:使用 tf.while_loop 在 tf.function 中处理具有不同形状张量的列表
- java - Appium 脚本为 Android 应用程序抛出“线程“主”java.lang.NoClassDefFoundError:org/objectweb/asm/Type 中的异常”
- javascript - 在无序整数数组中找到最大对和的函数