-2

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


这是 Form1.Designer.cs 的代码。错误显示在线 base.Dispose(disposing)。基本上我不知道这部分代码是做什么的。我无法修复它。有人可以告诉我,它是如何工作的吗?这部分什么时候调用?我在我的程序中没有发现 Dispose() 方法的任何用途,仅覆盖每个 Control 中的方法(从 UserControl 继承)。谢谢

namespace WindowsFormsApplication1 { partial class Form1 { /// /// 必需的设计器变量。/// 私有 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=Cross-thread operation not valid: Control 'Form1' 从创建它的线程以外的线程访问。Source=System.Windows.Forms StackTrace:在 System.Windows.Forms.Control.get_Handle() 在 System.Windows.Forms.Form.UpdateMenuHandles(MainMenu menu, Boolean forceRedraw) 在 System.Windows.Forms.Form.UpdateMenuHandles()在 System.Windows.Forms.Form.MenuChanged(Int32 change, Menu menu) at System.Windows.Forms.Form.Dispose(Boolean disposing) at WindowsFormsApplication1.Form1.Dispose(Boolean disposing) in c:__ Projects__\C#\RailJay 诊断 SW\20130408\Motor\Motor\Form1.Designer.cs:第 23 行在 System.ComponentModel.Component.Dispose() 在 System.Windows.Forms.ApplicationContext.Dispose(Boolean disposing) 在 System.Windows .Forms.Application.ThreadContext.DisposeThreadWindows() 内部异常:

4

2 回答 2

1

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

于 2013-04-08T12:52:42.097 回答
0

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

[编辑]

正如吉姆所提到的,您正在从一个单独的线程中引用一个表单;

这是异常详细信息:

System.InvalidOperationException 未被用户代码处理 Message=Cross-thread operation not valid: Control 'Form1' 从创建它的线程以外的线程访问

这意味着您正试图从未在其上创建的线程中处理 Form1。

您是否在应用程序中运行了单独的线程,如果是,这是首先要查看的地方。

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

于 2013-04-08T12:13:51.923 回答