InvalidOperationalException base.Dispose(disposing)

本文关键字:disposing Dispose base InvalidOperationalException | 更新日期: 2023-09-27 18:08:15

为plc创建诊断应用程序。我有一个窗体和几个控件,这是继承UserControl。这个控制开关。一个视图是Dashboard、Graph等等。当图形运行时,发生异常。异常显示在Form1.Designer.cs中的"base.Dispose(处置)"行。你知道我该怎么修吗?谢谢你的建议。


这是Form1.Designer.cs的代码。在联机基础上显示错误。dispose(处置)。基本上我不知道这部分代码是做什么的。我无法修复它。有人能告诉我,这是怎么回事吗?这部分什么时候叫?我在我的程序中没有发现任何Dispose()方法的使用,只是在每个控件(从UserControl继承)中重写方法。由于

WindowsFormsApplication1

名称空间{部分类Form1{//////需要的设计器变量。///private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        /*Updated 8.4.2013*/
        //try
        //{
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        //}
        //catch { }
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.statusStrip1 = new System.Windows.Forms.StatusStrip();
        this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
        this.btn_ChangeLanguage = new System.Windows.Forms.Button();
        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.Zobraz_Palubka = new System.Windows.Forms.Button();
        this.Zobraz_Verzi = new System.Windows.Forms.Button();
        this.Zobraz_error = new System.Windows.Forms.Button();
        this.Zobraz_Nastaveni = new System.Windows.Forms.Button();
        this.Zobraz_TestIO = new System.Windows.Forms.Button();
        this.Zobraz_monitor = new System.Windows.Forms.Button();
        this.statusStrip1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
        this.SuspendLayout();
        // 
        // statusStrip1
        // 
        this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.toolStripStatusLabel1});
        resources.ApplyResources(this.statusStrip1, "statusStrip1");
        this.statusStrip1.Name = "statusStrip1";
        // 
        // toolStripStatusLabel1
        // 
        this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
        resources.ApplyResources(this.toolStripStatusLabel1, "toolStripStatusLabel1");
        // 
        // btn_ChangeLanguage
        // 
        this.btn_ChangeLanguage.Image = global::WindowsFormsApplication1.Properties.Resources.flag_cze_mini;
        resources.ApplyResources(this.btn_ChangeLanguage, "btn_ChangeLanguage");
        this.btn_ChangeLanguage.Name = "btn_ChangeLanguage";
        this.btn_ChangeLanguage.UseVisualStyleBackColor = true;
        this.btn_ChangeLanguage.Click += new System.EventHandler(this.btn_ChangeLanguage_Click);
        this.btn_ChangeLanguage.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.btn_ChangeLanguage.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // pictureBox1
        // 
        this.pictureBox1.BackColor = System.Drawing.Color.Transparent;
        this.pictureBox1.Image = global::WindowsFormsApplication1.Properties.Resources.trojuhelnik;
        resources.ApplyResources(this.pictureBox1, "pictureBox1");
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.TabStop = false;
        // 
        // Zobraz_Palubka
        // 
        this.Zobraz_Palubka.Image = global::WindowsFormsApplication1.Properties.Resources.tachometr_mini;
        resources.ApplyResources(this.Zobraz_Palubka, "Zobraz_Palubka");
        this.Zobraz_Palubka.Name = "Zobraz_Palubka";
        this.Zobraz_Palubka.UseVisualStyleBackColor = true;
        this.Zobraz_Palubka.Click += new System.EventHandler(this.Zobraz_Click);
        this.Zobraz_Palubka.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.Zobraz_Palubka.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // Zobraz_Verzi
        // 
        this.Zobraz_Verzi.Image = global::WindowsFormsApplication1.Properties.Resources.info_mini;
        resources.ApplyResources(this.Zobraz_Verzi, "Zobraz_Verzi");
        this.Zobraz_Verzi.Name = "Zobraz_Verzi";
        this.Zobraz_Verzi.UseVisualStyleBackColor = true;
        this.Zobraz_Verzi.Click += new System.EventHandler(this.Zobraz_Click);
        this.Zobraz_Verzi.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.Zobraz_Verzi.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // Zobraz_error
        // 
        this.Zobraz_error.Image = global::WindowsFormsApplication1.Properties.Resources.error1_mini;
        resources.ApplyResources(this.Zobraz_error, "Zobraz_error");
        this.Zobraz_error.Name = "Zobraz_error";
        this.Zobraz_error.UseVisualStyleBackColor = true;
        this.Zobraz_error.Click += new System.EventHandler(this.Zobraz_Click);
        this.Zobraz_error.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.Zobraz_error.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // Zobraz_Nastaveni
        // 
        this.Zobraz_Nastaveni.Image = global::WindowsFormsApplication1.Properties.Resources.settings_mini;
        resources.ApplyResources(this.Zobraz_Nastaveni, "Zobraz_Nastaveni");
        this.Zobraz_Nastaveni.Name = "Zobraz_Nastaveni";
        this.Zobraz_Nastaveni.UseVisualStyleBackColor = true;
        this.Zobraz_Nastaveni.Click += new System.EventHandler(this.Zobraz_Click);
        this.Zobraz_Nastaveni.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.Zobraz_Nastaveni.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // Zobraz_TestIO
        // 
        this.Zobraz_TestIO.Image = global::WindowsFormsApplication1.Properties.Resources.testIO_mini;
        resources.ApplyResources(this.Zobraz_TestIO, "Zobraz_TestIO");
        this.Zobraz_TestIO.Name = "Zobraz_TestIO";
        this.Zobraz_TestIO.UseVisualStyleBackColor = true;
        this.Zobraz_TestIO.Click += new System.EventHandler(this.Zobraz_Click);
        this.Zobraz_TestIO.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.Zobraz_TestIO.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // Zobraz_monitor
        // 
        this.Zobraz_monitor.Image = global::WindowsFormsApplication1.Properties.Resources.line_graph_icon_mini;
        resources.ApplyResources(this.Zobraz_monitor, "Zobraz_monitor");
        this.Zobraz_monitor.Name = "Zobraz_monitor";
        this.Zobraz_monitor.UseVisualStyleBackColor = true;
        this.Zobraz_monitor.Click += new System.EventHandler(this.Zobraz_Click);
        this.Zobraz_monitor.MouseEnter += new System.EventHandler(this.Zobraz_Nastaveni_MouseEnter);
        this.Zobraz_monitor.MouseLeave += new System.EventHandler(this.Zobraz_Nastaveni_MouseLeave);
        // 
        // Form1
        // 
        resources.ApplyResources(this, "$this");
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.btn_ChangeLanguage);
        this.Controls.Add(this.pictureBox1);
        this.Controls.Add(this.Zobraz_Palubka);
        this.Controls.Add(this.Zobraz_Verzi);
        this.Controls.Add(this.Zobraz_error);
        this.Controls.Add(this.statusStrip1);
        this.Controls.Add(this.Zobraz_Nastaveni);
        this.Controls.Add(this.Zobraz_TestIO);
        this.Controls.Add(this.Zobraz_monitor);
        this.Name = "Form1";
        this.statusStrip1.ResumeLayout(false);
        this.statusStrip1.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    #endregion
    private System.Windows.Forms.Button Zobraz_monitor;
    private System.Windows.Forms.Button Zobraz_Nastaveni;
    private System.Windows.Forms.StatusStrip statusStrip1;
    private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
    private System.Windows.Forms.Button Zobraz_TestIO;
    private System.Windows.Forms.Button Zobraz_error;
    private System.Windows.Forms.Button Zobraz_Verzi;
    private System.Windows.Forms.Button Zobraz_Palubka;
    private System.Windows.Forms.PictureBox pictureBox1;
    private System.Windows.Forms.Button btn_ChangeLanguage;
}

}


下面是异常细节:

系统。InvalidOperationException未被用户代码处理消息=跨线程操作无效:控制'Form1'从创建它的线程以外的线程访问。源= System.Windows.Forms加:在System.Windows.Forms.Control.get_Handle ()在System.Windows.Forms.Form。UpdateMenuHandles(MainMenu菜单,Boolean forceRedraw)在System.Windows.Forms.Form.UpdateMenuHandles ()在System.Windows.Forms.Form。MenuChanged(Int32更改,菜单菜单)在System.Windows.Forms.Form。处理(布尔处理)在WindowsFormsApplication1.Form1。Dispose(Boolean Dispose) in c:__Projects__' c# 'RailJay Diagnostic SW'20130408'Motor'Motor' form . designer .cs:line 23在System.ComponentModel.Component.Dispose ()在System.Windows.Forms.ApplicationContext。处理(布尔处理)在System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows ()InnerException:

InvalidOperationalException base.Dispose(disposing)

看起来您正在从GUI线程以外的线程调用Dispose。你不能那样做。

您需要更具体-也许添加一些代码到您要做的事情,在哪里?

[编辑]

正如Jim提到的,你从一个单独的线程引用一个表单;

下面是异常细节:

系统。InvalidOperationException未被用户代码处理消息=跨线程操作无效:访问控件"Form1"从在

上创建它的线程以外的线程

这意味着你试图从一个不是创建Form1的线程中删除Form1。

应用程序中是否有单独的线程在运行,如果有,这是第一个要查看的地方。

你是否在代码的任何地方关闭Form1 ?