wild84
wild84
采纳率91.6%
2020-01-08 09:17

C#委託事件如何更新UI的值

5
已采纳

我想在一個委託事件中更新ui值,但是總是報錯,請幫我看看:

System.InvalidOperationException: '跨執行緒作業無效: 存取控制項 'tb_log' 時所使用的執行緒與建立控制項的執行緒不同。'

我的代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MavenInstall
{
    public partial class MavenInstall : Form
    {
        public delegate void OutDelegate(string txt);
        public OutDelegate outDelegate;
        public MavenInstall()
        {
            InitializeComponent();
            tb_packaging.Text = "jar";
            outDelegate = UpdateLog;
            //tb_command.Text = "mvn install:install-file -DgroupId={0} -DartifactId={1} -Dversion={2} -Dpackaging={3} -Dfile={4}";
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (SelectJarFile.ShowDialog() == DialogResult.OK)
            {
                tb_file.Text = SelectJarFile.FileName;
            }
        }

        private void btn_execute_Click(object sender, EventArgs e)
        {
            tb_log.Text = null;
            foreach (Control control in gb_condition.Controls)
            {
                control.Enabled = false;
            }
            String cmd = String.Format(@"mvn install:install-file -DgroupId={0} -DartifactId={1} -Dversion={2} -Dpackaging={3} -Dfile={4}"
                , tb_groupid.Text,tb_artifactid.Text
                ,tb_version.Text,tb_packaging.Text,tb_file.Text);
            tb_command.Text = cmd;
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
            process.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
            process.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
            process.StartInfo.RedirectStandardError = true;//重定向标准错误输出
            process.StartInfo.CreateNoWindow = true;//不显示程序窗口
            process.Start();//启动程序

            process.OutputDataReceived += new DataReceivedEventHandler(OutputDataHandler);
            process.BeginOutputReadLine();

            process.StandardInput.WriteLine(cmd);
            process.StandardInput.AutoFlush = true;
            process.StandardInput.WriteLine("exit");

            process.WaitForExit();//等待程序执行完退出进程
            foreach (Control control in gb_condition.Controls)
            {
                control.Enabled = true;
            }
            process.Close();
        }

        private void OutputDataHandler(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data)) {
                //UpdateLog(e.Data);
                outDelegate.Invoke(e.Data);
            }
        }

        private void UpdateLog(string txt)
        {
            //if (tb_log.InvokeRequired)
            //{

            //    tb_log.Invoke(outDelegate, txt);
            //}
            //else 
            //{
            //    this.tb_log.Text += txt + "\r\n";
            //}

            this.tb_log.Text += txt + "\r\n";
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            tb_groupid.Text = null;
            tb_artifactid.Text = null;
            tb_version.Text = null;
            tb_packaging.Text = null;
            tb_file.Text = null;
            tb_command.Text = null;
            tb_log.Text = null;
        }
    }
}

design文件

namespace MavenInstall
{
    partial class MavenInstall
    {
        /// <summary>
        /// 設計工具所需的變數。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清除任何使用中的資源。
        /// </summary>
        /// <param name="disposing">如果應該處置受控資源則為 true,否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form 設計工具產生的程式碼

        /// <summary>
        /// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改
        /// 這個方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btn_clear = new System.Windows.Forms.Button();
            this.btn_execute = new System.Windows.Forms.Button();
            this.gb_condition = new System.Windows.Forms.GroupBox();
            this.tb_command = new System.Windows.Forms.TextBox();
            this.btn_select = new System.Windows.Forms.Button();
            this.lb_Command = new System.Windows.Forms.Label();
            this.tb_file = new System.Windows.Forms.TextBox();
            this.lb_File = new System.Windows.Forms.Label();
            this.tb_packaging = new System.Windows.Forms.TextBox();
            this.lb_Packaging = new System.Windows.Forms.Label();
            this.tb_version = new System.Windows.Forms.TextBox();
            this.lb_Version = new System.Windows.Forms.Label();
            this.tb_artifactid = new System.Windows.Forms.TextBox();
            this.lb_ArtifactId = new System.Windows.Forms.Label();
            this.tb_groupid = new System.Windows.Forms.TextBox();
            this.lb_GroupId = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.tb_log = new System.Windows.Forms.TextBox();
            this.SelectJarFile = new System.Windows.Forms.OpenFileDialog();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.panel1.SuspendLayout();
            this.gb_condition.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Margin = new System.Windows.Forms.Padding(4);
            this.splitContainer1.Name = "splitContainer1";
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.panel1);
            this.splitContainer1.Panel1.Controls.Add(this.gb_condition);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.groupBox1);
            this.splitContainer1.Size = new System.Drawing.Size(1067, 372);
            this.splitContainer1.SplitterDistance = 348;
            this.splitContainer1.SplitterWidth = 5;
            this.splitContainer1.TabIndex = 0;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btn_clear);
            this.panel1.Controls.Add(this.btn_execute);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 328);
            this.panel1.Margin = new System.Windows.Forms.Padding(4);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(348, 44);
            this.panel1.TabIndex = 1;
            // 
            // btn_clear
            // 
            this.btn_clear.Location = new System.Drawing.Point(240, 8);
            this.btn_clear.Margin = new System.Windows.Forms.Padding(4);
            this.btn_clear.Name = "btn_clear";
            this.btn_clear.Size = new System.Drawing.Size(100, 29);
            this.btn_clear.TabIndex = 0;
            this.btn_clear.Text = "Clear(&C)";
            this.btn_clear.UseVisualStyleBackColor = true;
            this.btn_clear.Click += new System.EventHandler(this.btn_clear_Click);
            // 
            // btn_execute
            // 
            this.btn_execute.Location = new System.Drawing.Point(8, 8);
            this.btn_execute.Margin = new System.Windows.Forms.Padding(4);
            this.btn_execute.Name = "btn_execute";
            this.btn_execute.Size = new System.Drawing.Size(97, 29);
            this.btn_execute.TabIndex = 0;
            this.btn_execute.Text = "Execute(&E)";
            this.btn_execute.UseVisualStyleBackColor = true;
            this.btn_execute.Click += new System.EventHandler(this.btn_execute_Click);
            // 
            // gb_condition
            // 
            this.gb_condition.Controls.Add(this.tb_command);
            this.gb_condition.Controls.Add(this.btn_select);
            this.gb_condition.Controls.Add(this.lb_Command);
            this.gb_condition.Controls.Add(this.tb_file);
            this.gb_condition.Controls.Add(this.lb_File);
            this.gb_condition.Controls.Add(this.tb_packaging);
            this.gb_condition.Controls.Add(this.lb_Packaging);
            this.gb_condition.Controls.Add(this.tb_version);
            this.gb_condition.Controls.Add(this.lb_Version);
            this.gb_condition.Controls.Add(this.tb_artifactid);
            this.gb_condition.Controls.Add(this.lb_ArtifactId);
            this.gb_condition.Controls.Add(this.tb_groupid);
            this.gb_condition.Controls.Add(this.lb_GroupId);
            this.gb_condition.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gb_condition.Location = new System.Drawing.Point(0, 0);
            this.gb_condition.Margin = new System.Windows.Forms.Padding(4);
            this.gb_condition.Name = "gb_condition";
            this.gb_condition.Padding = new System.Windows.Forms.Padding(4);
            this.gb_condition.Size = new System.Drawing.Size(348, 372);
            this.gb_condition.TabIndex = 0;
            this.gb_condition.TabStop = false;
            this.gb_condition.Text = "Condition";
            // 
            // tb_command
            // 
            this.tb_command.Location = new System.Drawing.Point(92, 196);
            this.tb_command.Margin = new System.Windows.Forms.Padding(4);
            this.tb_command.Multiline = true;
            this.tb_command.Name = "tb_command";
            this.tb_command.ReadOnly = true;
            this.tb_command.Size = new System.Drawing.Size(247, 115);
            this.tb_command.TabIndex = 1;
            // 
            // btn_select
            // 
            this.btn_select.Location = new System.Drawing.Point(253, 161);
            this.btn_select.Margin = new System.Windows.Forms.Padding(4);
            this.btn_select.Name = "btn_select";
            this.btn_select.Size = new System.Drawing.Size(87, 29);
            this.btn_select.TabIndex = 0;
            this.btn_select.Text = "Select(&S)";
            this.btn_select.UseVisualStyleBackColor = true;
            this.btn_select.Click += new System.EventHandler(this.btn_select_Click);
            // 
            // lb_Command
            // 
            this.lb_Command.AutoSize = true;
            this.lb_Command.Location = new System.Drawing.Point(12, 202);
            this.lb_Command.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lb_Command.Name = "lb_Command";
            this.lb_Command.Size = new System.Drawing.Size(69, 15);
            this.lb_Command.TabIndex = 0;
            this.lb_Command.Text = "Command:";
            // 
            // tb_file
            // 
            this.tb_file.Location = new System.Drawing.Point(92, 161);
            this.tb_file.Margin = new System.Windows.Forms.Padding(4);
            this.tb_file.Name = "tb_file";
            this.tb_file.Size = new System.Drawing.Size(152, 25);
            this.tb_file.TabIndex = 1;
            // 
            // lb_File
            // 
            this.lb_File.AutoSize = true;
            this.lb_File.Location = new System.Drawing.Point(55, 168);
            this.lb_File.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lb_File.Name = "lb_File";
            this.lb_File.Size = new System.Drawing.Size(33, 15);
            this.lb_File.TabIndex = 0;
            this.lb_File.Text = "File:";
            // 
            // tb_packaging
            // 
            this.tb_packaging.Location = new System.Drawing.Point(92, 126);
            this.tb_packaging.Margin = new System.Windows.Forms.Padding(4);
            this.tb_packaging.Name = "tb_packaging";
            this.tb_packaging.Size = new System.Drawing.Size(247, 25);
            this.tb_packaging.TabIndex = 1;
            // 
            // lb_Packaging
            // 
            this.lb_Packaging.AutoSize = true;
            this.lb_Packaging.Location = new System.Drawing.Point(13, 132);
            this.lb_Packaging.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lb_Packaging.Name = "lb_Packaging";
            this.lb_Packaging.Size = new System.Drawing.Size(69, 15);
            this.lb_Packaging.TabIndex = 0;
            this.lb_Packaging.Text = "Packaging:";
            // 
            // tb_version
            // 
            this.tb_version.Location = new System.Drawing.Point(92, 91);
            this.tb_version.Margin = new System.Windows.Forms.Padding(4);
            this.tb_version.Name = "tb_version";
            this.tb_version.Size = new System.Drawing.Size(247, 25);
            this.tb_version.TabIndex = 1;
            // 
            // lb_Version
            // 
            this.lb_Version.AutoSize = true;
            this.lb_Version.Location = new System.Drawing.Point(29, 98);
            this.lb_Version.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lb_Version.Name = "lb_Version";
            this.lb_Version.Size = new System.Drawing.Size(55, 15);
            this.lb_Version.TabIndex = 0;
            this.lb_Version.Text = "Version:";
            // 
            // tb_artifactid
            // 
            this.tb_artifactid.Location = new System.Drawing.Point(92, 56);
            this.tb_artifactid.Margin = new System.Windows.Forms.Padding(4);
            this.tb_artifactid.Name = "tb_artifactid";
            this.tb_artifactid.Size = new System.Drawing.Size(247, 25);
            this.tb_artifactid.TabIndex = 1;
            // 
            // lb_ArtifactId
            // 
            this.lb_ArtifactId.AutoSize = true;
            this.lb_ArtifactId.Location = new System.Drawing.Point(17, 62);
            this.lb_ArtifactId.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lb_ArtifactId.Name = "lb_ArtifactId";
            this.lb_ArtifactId.Size = new System.Drawing.Size(67, 15);
            this.lb_ArtifactId.TabIndex = 0;
            this.lb_ArtifactId.Text = "ArtifactId:";
            // 
            // tb_groupid
            // 
            this.tb_groupid.Location = new System.Drawing.Point(92, 21);
            this.tb_groupid.Margin = new System.Windows.Forms.Padding(4);
            this.tb_groupid.Name = "tb_groupid";
            this.tb_groupid.Size = new System.Drawing.Size(247, 25);
            this.tb_groupid.TabIndex = 1;
            // 
            // lb_GroupId
            // 
            this.lb_GroupId.AutoSize = true;
            this.lb_GroupId.Location = new System.Drawing.Point(24, 26);
            this.lb_GroupId.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
            this.lb_GroupId.Name = "lb_GroupId";
            this.lb_GroupId.Size = new System.Drawing.Size(59, 15);
            this.lb_GroupId.TabIndex = 0;
            this.lb_GroupId.Text = "GroupId:";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.tb_log);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Margin = new System.Windows.Forms.Padding(4);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Padding = new System.Windows.Forms.Padding(4);
            this.groupBox1.Size = new System.Drawing.Size(714, 372);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Log";
            // 
            // tb_log
            // 
            this.tb_log.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tb_log.Location = new System.Drawing.Point(4, 22);
            this.tb_log.Margin = new System.Windows.Forms.Padding(4);
            this.tb_log.Multiline = true;
            this.tb_log.Name = "tb_log";
            this.tb_log.ReadOnly = true;
            this.tb_log.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.tb_log.Size = new System.Drawing.Size(706, 346);
            this.tb_log.TabIndex = 0;
            // 
            // SelectJarFile
            // 
            this.SelectJarFile.FileName = "SelectJarFile";
            // 
            // MavenInstall
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1067, 372);
            this.Controls.Add(this.splitContainer1);
            this.Margin = new System.Windows.Forms.Padding(4);
            this.Name = "MavenInstall";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "MavenInstall";
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.gb_condition.ResumeLayout(false);
            this.gb_condition.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button btn_clear;
        private System.Windows.Forms.Button btn_execute;
        private System.Windows.Forms.GroupBox gb_condition;
        private System.Windows.Forms.TextBox tb_command;
        private System.Windows.Forms.Button btn_select;
        private System.Windows.Forms.Label lb_Command;
        private System.Windows.Forms.TextBox tb_file;
        private System.Windows.Forms.Label lb_File;
        private System.Windows.Forms.TextBox tb_packaging;
        private System.Windows.Forms.Label lb_Packaging;
        private System.Windows.Forms.TextBox tb_version;
        private System.Windows.Forms.Label lb_Version;
        private System.Windows.Forms.TextBox tb_artifactid;
        private System.Windows.Forms.Label lb_ArtifactId;
        private System.Windows.Forms.TextBox tb_groupid;
        private System.Windows.Forms.Label lb_GroupId;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox tb_log;
        private System.Windows.Forms.OpenFileDialog SelectJarFile;
    }
}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 1年前
     private void UpdateLog(string txt)
    {
    this.Invoke(new delegate() {
    
    this.tb_log.Text += txt + "\r\n";
        });
    }
    
    点赞 1 评论 复制链接分享