无法访问已处置的对象

本文关键字:对象 访问 | 更新日期: 2023-09-27 18:10:27

我在加载页面中有一个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();
         }

在我的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)
        {
        }
    }

我的问题是当我想显示另一种形式时,我得到了这个错误:

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

这个错误

  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}

我在GetOnlineTrain方法中得到了这个错误。

FrmPathformload当我把这行我得到了上面的错误,但当我清除这行一切都很好!!!!!!!

private void frmLine_Load(object sender, EventArgs e)
{
    txtNumber.Focus();
    gvListLine.DataSource = objLineRepository.GetAll().ToList();
 }

敬礼

无法访问已处置的对象

你的线程循环无尽(因为while (true))。所以一旦你关闭你的表单(我假设当你"想要显示另一个表单"你关闭你的旧表单),并调用Dispose对它(可能由框架完成)。然而,线程将继续使用该表单,因为它不会停止。下次调用Invoke时,它将崩溃

我就是这么做的。

//解决退出表单时崩溃的代码

        // 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);
        }

//整个表单

   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);
            }
    
    
    
    
    
    
        }
    
    
    
    
    }