Reputation: 184
I have a form
in load page i create a thread and delegate to show a position that every time is updated :
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:
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 .
I FrmPath
in formload
when i put this line i got the above error ,but when i clear this line every thing works fine!!!!!!!
private void frmLine_Load(object sender, EventArgs e)
{
txtNumber.Focus();
gvListLine.DataSource = objLineRepository.GetAll().ToList();
}
Best regards
Upvotes: 0
Views: 4249
Reputation:
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);
}
}
}
Upvotes: 1
Reputation: 1051
Your thread loops endless (because of the 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). The thread however will continue working with that form since it will not stop. The next time it calls Invoke
it will crash.
Upvotes: 1