开发者

How can I fix a NullReferenceException in System.Windows.Forms.ToolStripControlHost.SetVisibleCore?

When setting control visibility I sometimes get this:

CRASH! Failed to refresh job control:
Exception:NullReferenceException
Message:Object reference not set to an instance of an object.
Source:     System.Windows.Forms
TargetSite: SetVisibleCore
StackTrace:    at System.Windows.Forms.ToolStripControlHost.SetVisibleCore(Boolean visible)
   at System.Windows.Forms.ToolStripItem.set_Visible(Boolean value)
   at Cwc.WaterWorks.Console.frmMain.SetToolStripOptionalButtons()
   at Cwc.WaterWorks.Console.frmMain.UpdateToolStrip(ToolStripButton toolStripButton)
   at Cwc.WaterWorks.Console.frmMain.RefreshJobControl(Boolean fetch)

Called from:

private void SetToolStripOptionalButtons()
{

    NewJobButtonsVisibility(newJobStripButton.Checked);
    JobListButtonsVisibility(jobListStripButton.Checked);
    jumpToJob.Visible = !newJobStripButton.Checked;

    if (newJobStripButton.Checked)
    {
        JobCreateStatus(false);
            uxLockedToolStrip.Visible = false;
        HideJobControlButtons();
    }
    else if (jobListStripButton.Checked)
    {
        UpdateFilterStatus();
        HideJobControlButtons();
    }
        else if (controlStripButton.Checked)
    {
        ShowJobControlButtons();
        }

        findCustomerStripButton.Enabled = newJo开发者_StackOverflowbStripButton.Checked && basis2ServiceLayer.Basis2Status.IsConnected==true;
    filterStripDropDown.Enabled = jobListStripButton.Checked;
}

Any ideas why?


I can repro this crash with a little test form:

public partial class Form1 : Form {
    public Form1() {
        var strip = new ToolStrip();
        test = new ToolStripControlHost(new TextBox());
        strip.Items.Add(test);
        this.Controls.Add(strip);
    }
    protected override void OnMouseClick(MouseEventArgs e) {
        test.Dispose();
        test.Visible = true;
    }
    ToolStripItem test;
}

Click the window and kaboom:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.ToolStripControlHost.SetVisibleCore(Boolean visible)
       at WindowsFormsApplication1.Form1.OnMouseClick(MouseEventArgs e) in C:\Users\hpass_000\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs:line 21
       ...
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at WindowsFormsApplication1.Program.Main() in C:\Users\hpass_000\AppData\Local\Temporary Projects\WindowsFormsApplication1\Program.cs:line 15

So you are trying to update your Toolstrip item after it was disposed. It isn't otherwise clear how your program got in a state like this. Very high odds for a threading problem, a thread that keeps invoking even after the user closed the window. The subject of this answer.

You really ought to tackle the core problem but a Q&D fix that will probably solve your crash is:

private void SetToolStripOptionalButtons()
{
    if (this.Disposed) return;
    // etc...
}

Albeit that it may now well crash elsewhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜