InvalidOperationException base.Dispose(disposing)

时间:2013-04-08 12:05:18

标签: c# exception dispose

我为PLC创建诊断应用程序。我有一个表单和几个控件,它们继承了UserControl。这控制我切换。一种视图是仪表板,图形等。图表运行时,会发生异常。 Form1.Designer.cs中的“base.Dispose(disposing)”行显示异常。你有什么想法,我怎么解决它?谢谢你的任何想法。


这是Form1.Designer.cs的代码。错误显示在行base.Dispose(disposing)上。 Basicaly我不知道这部分代码是做什么的。我无法修复它。有人可以告诉我,它是如何工作的?这个部分何时被称为?我没有在我的程序中找到任何方法Dispose(),只重写每个Control中的方法(继承自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;
}

}


以下是例外情况:

System.InvalidOperationException未被用户代码处理   Message =跨线程操作无效:控制从其创建的线程以外的线程访问的“Form1”。   来源= System.Windows.Forms的   堆栈跟踪:        在System.Windows.Forms.Control.get_Handle()        在System.Windows.Forms.Form.UpdateMenuHandles(MainMenu菜单,布尔forceRedraw)        在System.Windows.Forms.Form.UpdateMenuHandles()        在System.Windows.Forms.Form.MenuChanged(Int32更改,菜单菜单)        在System.Windows.Forms.Form.Dispose(布尔处理)        在WindowsFormsApplication1.Form1.Dispose(布尔设置在c)中:__ 项目 __ \ C#\ RailJay诊断SW \ 20130408 \电机\电机\ Form1.Designer.cs:第23行        在System.ComponentModel.Component.Dispose()        在System.Windows.Forms.ApplicationContext.Dispose(布尔处理)        在System.Windows.Forms.Application.ThreadContext.DisposeThreadWindows()   InnerException:

2 个答案:

答案 0 :(得分:1)

您似乎是从GUI线程以外的线程调用Dispose。你不能这样做。

答案 1 :(得分:0)

您需要更加具体 - 可能会在您尝试做的事情中添加一些代码,在哪里?

[编辑]

正如吉姆所说,你是从一个单独的线程引用一个表单;

  

以下是例外情况:

     

System.InvalidOperationException未被用户代码处理   消息=跨线程操作无效:访问控制'Form1'   来自其在

上创建的线程以外的线程

这意味着您正在尝试从未创建的线程中处置Form1。

你是否在应用程序中运行了sperate线程,如果有,这是第一个看的地方。

您是否在代码中的任何位置关闭Form1?