I have 2 projects in my solution. One is my Launcher (Relevant Code Only):
namespace Launcher
{
public partial class LauncherForm: Form
{
private static MyApplication.AppForm? AppForm = null;
private void OpenAppButton_Click(object sender, EventArgs e)
{
try
{
if(AppForm is null || AppForm.IsDisposed)
AppForm = new();
AppForm.FormClosed += AppForm_FormClosed;
AppForm.Show();
AppForm.Focus();
}
catch(Exception e)
{
Logger.Error(e);
}
}
private void AppForm_FormClosed(object sender, FormClosedEventArgs e)
{
this.Focus(); //Bring LauncherForm to the front
}
}
}
... and the other is my Application (Relevant Code Only):
namespace MyApplication
{
public partial class AppForm : Form
{
private static DebugLog? = null;
public AppForm()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(AppForm_FormClosing);
}
private void AppForm_FormClosing(object? sender, FormClosingEventArgs e)
{
DebugLog?.Close();
}
private void ShowDebugLogButton_Click(object sender, EventArgs e)
{
if(DebugLog is null || DebugLog.IsDisposed)
DebugLog = new();
using (FileStream fs = new(DebugLogFullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using StreamReader sr = new(fs);
contents = sr.ReadToEnd();
}
DebugLog.LogViewerTextBox.Text = contents;
DebugLog.Show();
DebugLog.LogViewerTextBox.SelectionStart = DebugLog.LogViewerTextBox.TextLength;
DebugLog.LogViewerTextBox.ScrollToCaret();
}
}
public partial class DebugLog : Form
{
[...]
}
}
Completely at random times while either opening the DebugLog or when closing the application I will get an uncatchable exception "Cannot access a disposed object. Object name: 'Panel'." at
DebugLog.Show();
or
this.Focus(); //Bring LauncherForm to the front
I have coded around in circles trying to check for nulls, disposed, disposing and can't seem to eliminate the issue.