简体   繁体   English

调用原因无法访问已处置的对象

[英]invoke causes Cannot access a disposed object

I have a form in load page i create a thread and delegate to show a position that every time is updated :我在加载页面中有一个form ,我创建了一个线程并委托以显示每次更新的位置:

   private delegate void UpdateListBoxDelegate();
  private UpdateListBoxDelegate UpdateListBox = null;

private void frmMain_Load(object sender, EventArgs e)
        {
             pictureBoxonlineTrain.Parent = pictureBoxMetroMap;
            UpdateListBox = new UpdateListBoxDelegate(this.UpdateStatus);
            // Initialise and start worker thread
            workerThread = new Thread(new ThreadStart(this.GetOnlineTrain));
            workerThread.Start();
         }

So in my load form i called this by thread:所以在我的load form我通过线程调用了它:

   private bool stop = false;
    public void GetOnlineTrain()
    {
        try
        {
            while (stop!=true)
            {
                TimeTableRepository objTimeTableREpository = new TimeTableRepository();
                OnlineTrainList = objTimeTableREpository.GetAll().ToList();
                objTimeTableREpository = null;
                if(stop!=true)
                Invoke(UpdateListBox);
                else this.Dispose();


            }
        }
        catch(Exception a)
        {
        }

    }

My problem is when i want to show another form i got this error:我的问题是当我想显示另一个表单时出现此错误:

        stop = true;
        frmPath frmPath = new frmPath();
        frmPath.ShowDialog();

This error这个错误

  Cannot access a disposed object  -{System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'frmMain'.
   at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
   at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
   at System.Windows.Forms.Control.Invoke(Delegate method)
   at PresentationLayer.PreLayer.frmMain.GetOnlineTrain() in d:\TFS Project\Railway\ShirazMetro\PresentationLayer\PreLayer\frmMain.cs:line 167}

I got this error in GetOnlineTrain method .我在GetOnlineTrain方法中收到此错误。

I FrmPath in formload when i put this line i got the above error ,but when i clear this line every thing works fine!!!!!!!FrmPathformload当我把此行中我得到了上面的错误,但是当我清除此行的每一件事工作正常!!!!!!!

private void frmLine_Load(object sender, EventArgs e)
{
    txtNumber.Focus();

    gvListLine.DataSource = objLineRepository.GetAll().ToList();
 }

Best regards此致

Your thread loops endless (because of the while (true) ).您的线程无限循环(因为while (true) )。 So once you close your form (I assume when you "want to show another form" you close your old form) and call Dispose on it (probably done by the framework).因此,一旦您关闭表单(我假设当您“想要显示另一个表单”时您关闭旧表单)并对其调用 Dispose(可能由框架完成)。 The thread however will continue working with that form since it will not stop.然而,线程将继续使用该表单,因为它不会停止。 The next time it calls Invoke it will crash.下次它调用Invoke它会崩溃。

This is what worked for me.这对我有用。

// THE CODE THAT SOLVED THE CRASH ON EXITING THE FORM // 解决退出表单时崩溃的代码

        // Aborts the Thread On Form Close // Stops Crashing on Form CLose 
        protected override void OnClosing(CancelEventArgs e)
        {
            if(captureThread.IsAlive)
            {
                captureThread.Abort();
            }

            base.OnClosing(e);
        }

// THE WHOLE FORM // 整个表格

   using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace ScreenRecorder1
    {
        public partial class Form1 : Form
        {
            Thread captureThread; // The Thread
    
    
    
            public Form1()
            {
                InitializeComponent();
             
    
            }
    
    
    
    
    
    
            private void Form1_Load(object sender, EventArgs e)
            {     
                captureThread = new Thread(CaptureScreen);
                captureThread.IsBackground = true;
                captureThread.Start();     
            }
    
    
    
    
    
    
            private void screenshot_bbutton_Click(object sender, EventArgs e)
            {
                CaptureScreen();
            }
    
    
    
    
    
    
    
            private void CaptureScreen()
            {
               
                while(true)
                {
                       Rectangle screenBounds = Screen.GetBounds(Point.Empty);  // Screen Size
                       Bitmap bmp1 = new Bitmap(screenBounds.Width, screenBounds.Height); // BMP
    
                       Graphics g = Graphics.FromImage(bmp1); // Graphics
                       g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds.Size); // Screen To BMP
                  
                  
                       Invoke(new Action(() => { Player_pictureBox.BackgroundImage = bmp1; })); // Display    
    
                       g.Dispose();
                      
                       Thread.Sleep(50);
                      
                }
    
             
                         
            }
    
    
    
    
            // Aborts the Thread On Form Close // Stops Crashing on Form CLose
            protected override void OnClosing(CancelEventArgs e)
            {
                if(captureThread.IsAlive)
                {
                    captureThread.Abort();
                }
    
                base.OnClosing(e);
            }
    
    
    
    
    
    
        }
    
    
    
    
    }

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

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