0

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.

Taldren
  • 11
  • 2
  • You may try unsubscribing from the events that you've subscribed to. – Tu deschizi eu inchid Jun 07 '23 at 04:25
  • @Tudeschizieuinchid I tried this and it didn't work, however, it did lead me onto the trail of what the cause was. As I was going through the process of unattaching and reattaching my handlers I finally noticed that I was using the 'FormClosed' event rather than that 'FormClosing' event. When I changed this to using 'FormClosing' the issues was resolved. I dont know this for sure, but I feel that there was a race condition of the 'FormClosed' event handler and it detatching all event handlers from the object being closed that sometimes did not give it enough time to complete. – Taldren Jun 08 '23 at 11:23
  • Actually, NM, that just made it less likely to happen. I just encountered it again. This really is maddening. – Taldren Jun 09 '23 at 00:45

0 Answers0