简体   繁体   English

在winform应用程序中关闭表单时如何修复“System.ObjectDisposedException”

[英]How to fix "System.ObjectDisposedException" when closing a form in winform application

I want to play a video in openGL using openTK.我想使用 openTK 在 openGL 中播放视频。 It is working fine.它工作正常。 but when closing that particular form it throws an exception "System.ObjectDisposedException: 'Cannot access a disposed object.'" under the code "while (glControl1.IsIdle)".但是当关闭该特定表单时,它会在代码“while (glControl1.IsIdle)”下引发异常“System.ObjectDisposedException:'无法访问已处理的对象。'”。 How can I resolve it?我该如何解决? My app contains a 'Form1'.我的应用程序包含一个“Form1”。 When user clicks a button on it, a 'Form2' will open as new window.当用户单击其上的按钮时,“Form2”将作为新窗口打开。 This Form2 will play video in openGL.这个 Form2 将在 openGL 中播放视频。

 private void Form2_Load(object sender, EventArgs e)
    {//openGL code
        StartCameras();
        glControl1.Resize += new EventHandler(glControl1_Resize);
        glControl1.Paint += new PaintEventHandler(glControl1_Paint);
        Application.Idle += Application_Idle;
        // Ensure that the viewport and projection matrix are set correctly.
        glControl1_Resize(glControl1, EventArgs.Empty);
    }
private void Application_Idle(object sender, EventArgs e)
    {
        while (glControl1.IsIdle)
        {
           Render();
        }
    }
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {        
        StopCameras();
    }
 private void StopCameras()
    {
        timer.Stop();
        videoSourcePlayer1.SignalToStop();
        videoSourcePlayer1.WaitForStop();           
    }

What about checking if it's disposed?:如何检查它是否已处理?:

while (!glControl1.IsDisposed && glControl1.IsIdle)

Assuming it inherits from the WinForms Control class, it should implement IsDisposed .假设它继承自 WinForms Control类,它应该实现IsDisposed

Try this if it could work.试试这个,如果它可以工作。

    private void Application_Idle(object sender, EventArgs e) { 
    try {while (glControl1.IsIdle) 
{ 
Render(); 
} 
    }
    Catch(Exception ex)
    //print the ex
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM