8

我有一个奇怪的问题,我发现很难调试有时我无法关闭我的程序,当我尝试关闭它时它会冻结。

我做了一个视频图像识别的大程序。我做了一个特殊的按钮来关闭相机。这个按钮通过调用下面的函数来工作,它确实有效。

 private void exitcamera()
 { 
 FinalVideo.SignalToStop();
 FinalVideo.WaitForStop();
 FinalVideo = null;
 }

请注意,原始视频是这样开始的

 private void buttonStartCamera_Click(object sender, EventArgs e) 
 {
 FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
 FinalVideo.DesiredFrameSize = new System.Drawing.Size(640, 480);
 FinalVideo.DesiredFrameRate = 90;
 FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
 FinalVideo.ProvideSnapshots = true;  //snapshots
 FinalVideo.Start(); 
 }

现在我的问题似乎(这是一个猜测,因为我现在无法调试)某个线程仍然处于活动状态,想要用数据更新主窗体。但是,它可能无法这样做,因为那个正在关闭。我认为类似的事情正在发生,所以我在主申请表上写了

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
     // Thread.Sleep(1000); // not sure about these delays might help syncing threads
     ExitCamera();
     Thread.Sleep(1000);
    }

然而,有了最后一个代码,程序退出的麻烦就更大了。

我想向子线程发送退出,但我不知道它们的名称(如果它们有名称),我不知道如何列出它们或指示它们停止它们在另一个 dll 中,而不是我的代码部分。从一些dll我没有代码。

那么有没有办法列出子线程,然后一一关闭,如果按右上角的右叉退出应用程序呢?

4

9 回答 9

13

好吧,我设法调试了程序,终于找到了导致问题的原因。这有点奇怪,因为作为一个按钮,我可以使用该exitcamera功能停止相机。

但是,在a _formclosing事件内部,尽管在我标记出该waitforstop功能后它起作用了,但相同的例程却不起作用。

 private void exitcamera()
 { 
  FinalVideo.SignalToStop();
  // FinalVideo.WaitForStop();  << marking out that one solved it
  FinalVideo.NewFrame -= new NewFrameEventHandler(FinalVideo_NewFrame); // as sugested
  FinalVideo = null;
 } 

我仍然对此感到有些困惑,为什么这在结束事件的情况下不起作用。但这似乎可以解决。

于 2012-12-10T13:53:03.597 回答
3

也许您有由事件引起的内存泄漏问题。您可以尝试在退出程序时取消挂钩事件:

FinalVideo.NewFrame -= new NewFrameEventHandler(FinalVideo_NewFrame);

也许这会有所帮助。

于 2012-12-08T19:33:47.513 回答
0

当我想显示预览时,这有助于解决另一个问题,单击“抓取”按钮,将相机的分辨率从低分辨率更改为高分辨率,抓取图像,然后改回低分辨率进行预览。这是有效的,即使我不得不放弃它,因为停止和启动相机会重置自动曝光,所以在抓取的图像上图片很糟糕:

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

public partial class Form1 : Form

{
    private int PreviewRefreshDelayMS = 40;
    private FilterInfoCollection VideoCaptureDevices;

    private VideoCaptureDevice CustomerWebcam;
    private int CustomerWebcam_CapabilitiesIndexMin;
    private int CustomerWebcam_CapabilitiesIndexMax;
    private bool bCustomerWebcam_capture;
    private Bitmap CustomerWebcam_bitmap;
    private System.DateTime CustomerWebcam_nextframetime = DateTime.Now;

    public Form1()
    {
        InitializeComponent();
    }

    // Some good info to make this more robust
    // http://haryoktav.wordpress.com/2009/03/21/webcam-in-c-aforgenet/
    //
    private void button1_Click(object sender, EventArgs e)
    {            
        CustomerWebcam = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
        CustomerWebcam.NewFrame += new NewFrameEventHandler(CustomerWebcam_NewFrame);

        int indexMin = -1;
        int MinPixels = 0;
        int indexMax = -1;
        int MaxPixels = 0;
        for (int i = 0; i < CustomerWebcam.VideoCapabilities.Length; i++)
        {
            int pixels = CustomerWebcam.VideoCapabilities[i].FrameSize.Height * CustomerWebcam.VideoCapabilities[i].FrameSize.Width; 
            if (indexMin == -1) { indexMin = i; MinPixels = pixels; }
            if (indexMax == -1) { indexMax = i; MaxPixels = pixels; }
            if (pixels < MinPixels) { indexMin = i; MinPixels = pixels; }
            if (pixels > MaxPixels) { indexMax = i; MaxPixels = pixels; }
        }
        CustomerWebcam_CapabilitiesIndexMin = indexMin;
        CustomerWebcam_CapabilitiesIndexMax = indexMax;

        CustomerWebcam.VideoResolution = CustomerWebcam.VideoCapabilities[indexMin];
        CustomerWebcam.DisplayPropertyPage(IntPtr.Zero);
        CustomerWebcam.Start();
    }

   void CustomerWebcam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        if (CustomerWebcam_bitmap != null)
        { 
            CustomerWebcam_bitmap.Dispose();
            CustomerWebcam_bitmap = null;
        }

        if (bCustomerWebcam_capture)
        {
            CustomerWebcam_bitmap = (Bitmap)eventArgs.Frame.Clone(); 
            System.Random rnd = new Random();
            CustomerWebcam_bitmap.Save("img" + Convert.ToString((int)(rnd.NextDouble() * 10000000)) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            bCustomerWebcam_capture = false;
            ((Bitmap)eventArgs.Frame).Dispose();
        }
        else
            if (DateTime.Now > CustomerWebcam_nextframetime)
        {
            CustomerWebcam_bitmap = (Bitmap)eventArgs.Frame.Clone();
            pictureBox1.Image = CustomerWebcam_bitmap;
            CustomerWebcam_nextframetime = DateTime.Now.AddMilliseconds(PreviewRefreshDelayMS);
            ((Bitmap)eventArgs.Frame).Dispose();
        }          
    }       


    private void Form1_Load(object sender, EventArgs e)
    {
        VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
        {
            comboBox1.Items.Add(VideoCaptureDevice.Name);
        }
        comboBox1.SelectedIndex = 0;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        CustomerWebcam.SignalToStop();
        CustomerWebcam = null;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!(CustomerWebcam == null))
            if (CustomerWebcam.IsRunning)
            {
                CustomerWebcam.SignalToStop();
                CustomerWebcam = null;
            }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        bCustomerWebcam_capture = true;
    }

}

还有一件事要提... AForge 库是我能够找到的最一致的方式,它可以使用网络摄像头抓取静止图像并保存为 JPEG,而无需深入研究 Windows 8 Metro 应用程序的世界。我希望使用 OpenCV.NET,或者只是使用 DirectShow 或 WIA 的常规 .NET API,但这是最简单的,它对我有用。

这里有一些很难找到但非常有用的好样本: https ://github.com/mdavid/aforge.net

于 2014-09-18T06:35:17.880 回答
0

在我的情况下,需要 WaitForStop() ,但代码执行在方法内部停止。我在调用 SignalToStop() 后立即将其替换为:

while (m_Device.IsRunning) { }

这是 AForge 库中涉及的代码:

public bool IsRunning
{
    get
    {
        if (this.thread != null)
        {
            if (!this.thread.Join(0))
            {
                return true;
            }
            this.Free();
        }
        return false;
    }
}

public void WaitForStop()
{
    if (this.thread != null)
    {
        this.thread.Join();
        this.Free();
    }
}

编辑:这并没有解决 100% 的问题。有时,对 WorkerThread() 方法上的 com 对象 (mediaControl.Stop();) 的调用只需要很长时间。

于 2017-06-13T14:40:45.013 回答
0

我正在处理这个问题。这是停止相机并关闭 Win Form 的简单方法。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (FinalVideo != null)
    {
        if (FinalVideo.IsRunning)
        {
            FinalVideo.SignalToStop();
            FinalVideo = null;
        }
    }
}
于 2017-02-08T14:52:45.167 回答
0

避免与表单直接交互 - 希望有比计时器更好的解决方案但解决问题。我

静态助手类

public static Bitmap StaticBitmap = new Bitmap(100,100);

形式

public void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {

        lock (StaticHelper.StaticBitmap)
        {
            using (Bitmap b = (Bitmap)eventArgs.Frame)
            {
                StaticHelper.StaticBitmap = (Bitmap)b.Clone();
            }

        }

    }

private void timer1_Tick(object sender, EventArgs e)
    {
        lock (StaticHelper.StaticBitmap)
        {
            pictureBox1.Image = (Bitmap)StaticHelper.StaticBitmap.Clone();
        }
    }
于 2019-01-26T04:02:49.060 回答
0

这会破坏你的问题(我有这个问题,我试过了)

using System.Threading;

bool photo_was_taken = false;


private void buttonStartCamera_Click(object sender, EventArgs e) 
 {

Thread thread = new Thread(new ThreadStart(exitcamera));
thread.Start(); 
 FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[comboBox1.SelectedIndex].MonikerString);
 FinalVideo.DesiredFrameSize = new System.Drawing.Size(640, 480);
 FinalVideo.DesiredFrameRate = 90;
 FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
 FinalVideo.ProvideSnapshots = true;  //snapshots
 FinalVideo.Start(); 
 }
private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        // what you want to do ( your code is here )
        photo_was_taken = true; 

    }
 private void exitcamera()
 { 
        while (!photo_was_taken)
        {
            Thread.Sleep(5); // you can change wait milliseconds
        }
        FinalVideo.SignalToStop();
        FinalVideo.NewFrame -= new NewFrameEventHandler(FinalVideo_NewFrame);
        //FinalVideo.WaitForStop();
        while (FinalVideo.IsRunning)
        {
            FinalVideo.Stop();
            // FinalVideo = null; >> // that is not condition
        }
 }
于 2019-03-27T20:48:49.943 回答
0

这就是您所需要的,100% 有效的解决方案:

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
    Invoke((MethodInvoker) delegate
    {
        _videoSource.SignalToStop();
        _videoSource.WaitForStop();
    });
}
于 2021-08-29T21:00:00.620 回答
-1

我尝试了一些解决方案,但没有任何效果。我部分解决了在 WaitForStop 之后添加线程睡眠

if (FinalVideo != null)
    {
        if (FinalVideo.IsRunning)
        {
            FinalVideo.SignalToStop();
            Thread.Sleep(1000);
        }
    }

如果我尝试调用停止应用程序将被冻结

于 2020-09-23T10:48:55.383 回答