我在加载页面中有一个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
方法中遇到此错误。
我在FrmPath
中formload
当我把这行放到上面时会出现上述错误,但是当我清除这一行时,每件事情都可以正常工作!!!!!!!
private void frmLine_Load(object sender, EventArgs e)
{
txtNumber.Focus();
gvListLine.DataSource = objLineRepository.GetAll().ToList();
}
祝你好运
答案 0 :(得分:1)
你的线程无休止地循环(因为while (true)
)。
所以一旦你关闭你的表单(我假设当你"想要显示另一个表单"你关闭旧表单)并在其上调用Dispose(可能由框架完成)。然而,线程将继续使用该表单,因为它不会停止。下次调用Invoke
时,它会崩溃。
答案 1 :(得分:1)
这对我有用。
//解决了删除表格时出现的代码
// 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);
}
}
}