C# 调用原因无法访问已释放的对象
我在加载页面中有一个C# 调用原因无法访问已释放的对象,c#,multithreading,winforms,forms,delegates,C#,Multithreading,Winforms,Forms,Delegates,我在加载页面中有一个表单,我创建了一个线程和委托,以显示每次更新的位置: private delegate void UpdateListBoxDelegate(); private UpdateListBoxDelegate UpdateListBox = null; private void frmMain_Load(object sender, EventArgs e) { pictureBoxonlineTrain.Parent =
表单
,我创建了一个线程和委托,以显示每次更新的位置:
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();
}
因此,在我的加载表单中
我通过线程将其称为:
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
方法中遇到此错误
我FrmPath
在formload
中,当我放这行时,我得到了上面的错误,但是当我清除这行时,一切都很好
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);
}
}
}
//整个表单
// 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);
}
}
}
从停止编写这样的代码开始:
catch(异常a){}
。它隐藏了您的真实错误,并且不帮助您的程序正确运行(即“无错误”不一定正确)。@Mehrdad:您需要重新设计GetOnlineTrain()
方法,完全提供一个“停止”条件,如while(!Stopped)
。此外,您还需要在表单的dispose方法中实际设置该标志,并且需要等待该标志存在。(如果做得不正确,这可能会导致各种死锁。但是,列出一个完整的解决方案会太复杂)我按照你说的那样做,请看更新后的帖子,但同样的错误。1<代码>停止从未设置。2.你不需要给自己打电话。3.主窗体需要等待线程在Dispose()重写内停止。@Mehrdad-请不要更新堆栈溢出的答案。只有在问题的编写方式存在问题时,才应编辑问题,或者通过在底部添加新信息来扩展问题。否则,如果问题发生变化,您可能会使其他用户很难理解答案如何适合问题。