【问题标题】:Passing variables between 2 forms, multithreading C#在 2 个表单之间传递变量,多线程 C#
【发布时间】:2016-05-14 07:12:25
【问题描述】:

我有 2 个表格:Form1 和 Form2。

Form1 包含一个按钮,用于调用 Form2 并在另一个线程上运行它。

Form2 包含 3 个复选框。当用户点击添加按钮时,它会生成一个字符串。

我的问题是如何将字符串传递给 Form1,然后将其添加到 Richtextbox?

谢谢。

Form1

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

namespace PassingData2Forms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void call_form_2()
        {
            for (int i = 0; i<10; i++) { 
            Form2 inst_form2 = new Form2();
            inst_form2.ShowDialog();
            }
        }

        private void f1_but_01_Click(object sender, EventArgs e)
        {
            Thread extra_thread_01 = new Thread(() => call_form_2());
            extra_thread_01.Start();            
        }
    }
}

Form2

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

namespace PassingData2Forms
{

    public partial class Form2 : Form
    { 
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private string clean_string(string process_string)
        {
            process_string = process_string.Replace(",,", ",");
            process_string = process_string.Trim(new char[] {','});
            return process_string;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] selected_array = new string[3];

            if (checkBox1.Checked == true)
            {
                selected_array[0] = "Summer";
            }

            if (checkBox2.Checked == true)
            {
                selected_array[1] = "Spring";
            }

            if (checkBox3.Checked == true)
            {
                selected_array[2] = "Fall";
            }

            string selected_string = clean_string(string.Join(",", selected_array));

            //---------------------------------------------------------------
            // How can I pass "selected_string" to RichTextBox in Form1 here?
            //---------------------------------------------------------------

            Close();
        }
    }
}

【问题讨论】:

  • 你能解释一下为什么你试图在不同的线程中运行你的第二个表单吗?
  • @Steve 因为这只是一个示例,所以我试图使其尽可能简单。我有一项繁重的任务需要大量时间来处理,并且必须在不同的线程上运行以避免冻结 GUI。如果我能找到这个示例的解决方案,我可以将它应用到我的真实应用中。
  • Form2 一定是一个表单还是只是一些后台线程?见stackoverflow.com/questions/661561/…
  • @LouisTran 在不同的线程中运行您的表单简单...通常,您在主线程上运行所有表单,实际上是所有 UI,并执行后台操作在工作线程/线程池上工作,然后将结果传回。

标签: c# multithreading winforms


【解决方案1】:

您可以将事件添加到以这种方式声明的 Form2 类中

public delegate void onMessageReady(string message);
public event onMessageReady MessageReady;

当您的 Form2 有消息准备好发送回有兴趣了解它的客户时,调用 MessageReady 事件

private void button1_Click(object sender, EventArgs e)
{
    .....
    string selected_string = clean_string(string.Join(",", selected_array));
    if(MessageReady != null) 
         MessageReady(selected_string);
    .....
}

最后一步是在构建 Form2 的实例时从 Form1 订阅事件

private void call_form_2()
{
    for (int i = 0; i<10; i++) { 
    Form2 inst_form2 = new Form2();
    inst_form2.MessageReady += MessageReceived;
    inst_form2.ShowDialog();
    }
}
private void MessageReceived(string message)
{
        if (form1RichTextBox.InvokeRequired)
           form1RichTextBox.Invoke(new Form2.onMessageReady(messageReady), new object[] {message});
        else
           form1RichTextBox.AppendText(message + Environment.NewLine);
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-11-15
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多