C# 运动传感器检测到网络摄像头流媒体系统:system.InvalidOperationException:';对象当前正在其他地方使用;

C# 运动传感器检测到网络摄像头流媒体系统:system.InvalidOperationException:';对象当前正在其他地方使用;,c#,streaming,webcam,picturebox,capture,C#,Streaming,Webcam,Picturebox,Capture,目前正在执行一个运动传感器检测项目,此错误有时并非每次都出现。运动传感器将检测运动,一旦传感器检测到运动,网络摄像头将从firebase数据库获取状态,然后激活网络摄像头并自动捕获图像并保存为JPG,播放语音消息通知用户,向用户发送电子邮件。然后在同一时间,图片框将开始为用户流。我可以一次添加到多个函数中吗?但我需要这个功能来实现我最后一年的项目目标 这是调用堆栈: 例外情况呢 我不知道如何解决这个问题,我需要从这里得到帮助。需要这个才能通过我的最后一个学期 using System; us

目前正在执行一个运动传感器检测项目,此错误有时并非每次都出现。运动传感器将检测运动,一旦传感器检测到运动,网络摄像头将从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();

    }
}
private async void interval(对象发送方,事件参数e)
{
FirebaseResponse=wait client.GetTaskAsync(“监控”);
Data1 obj=response.ResultAs();
if(obj.sens01==“X”&&trig1==0)
{
videoCaptureDevice=新的videoCaptureDevice(filterInfoCollection[comboBox1.SelectedIndex].MonitorString);
videoCaptureDevice.NewFrame+=videoCaptureDevice\u NewFrame;
videoCaptureDevice.Start();
trig1=1;
SoundPlayer splayer1=新的SoundPlayer(@“E:\FYP 2 edit 28April\cam 1.wav”);
八字游戏1;
sendMail1();
}
if(obj.sens02==“X”&&trig2==0)
{
videoCaptureDevice2=新的VideoCaptureDevice(filterInfoCollection[comboBox2.SelectedIndex].MonitorString);
videoCaptureDevice2.NewFrame+=VideoCaptureDevice_NewFrame2;
videoCaptureDevice2.Start();
trig2=1;
SoundPlayer splayer2=新的SoundPlayer(@“E:\FYP 2 edit 28April\cam 2.wav”);
splayer2.Play();
sendMail2();
}
if(obj.sens03==“X”&&trig3==0)
{
videoCaptureDevice3=新的VideoCaptureDevice(filterInfoCollection[comboBox3.SelectedIndex].MonitorString);
videoCaptureDevice3.NewFrame+=VideoCaptureDevice\u NewFrame3;
videoCaptureDevice3.Start();
trig3=1;
SoundPlayer splayer3=新的SoundPlayer(@“E:\FYP 2 edit 28April\cam 3.wav”);
八字游戏3.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());
            
            
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间安全性
{
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}

检查事件查看器,查看应用程序失败时是否发生任何错误。请添加堆栈跟踪,或者是否可以标记发生异常的行?另外,如果对问题没有帮助,请删除注释代码。好的,现在。你的很多功能都有副作用。你需要解释清楚。在您的时间间隔内,如果您收到一个触发器,您将开始录制,而不管设备的当前状态如何。你需要先检查一下,以确保你确实可以开始,或者需要重新开始。另一件事——可能是造成问题的原因,但不是问题的直接根源——是这个类的完全混乱。但是如果你已经没有时间了,你可能无法让这个“漂亮”。。。让它工作起来。嗨,伙计们,谢谢你们的快速评论。。。我刚刚更新了帖子。。。删除注释并在软件中断时添加异常。我可以用一种叫做“锁”的东西,对吗?但是我对它不熟悉,这有帮助吗?