来自构造函数的 C# 字符串未传递给表单

标签 c# forms constructor pipe program-entry-point

所以我编写了这个工具,它通过命名管道从另一个进程接收字符串。我想将从我的主要方法接收到的字符串传递给我的一个表单,但是每当我尝试使用字符串构造函数时,它的值似乎都是空的。

我有一种感觉,我对构造函数做错了什么,或者没有从我当前运行的表单中接收到字符串...

这是我在主类中捕获和发送字符串的方法:

public static async void startServer()
{

    Task.Factory.StartNew(() =>
        {

            using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("PipeToProgram"))
            {
                pipeStream.WaitForConnection();

                using (StreamReader sr = new StreamReader(pipeStream))
                {
                    string message;
                    while ((message = sr.ReadLine()) != null)
                    {
                        Form1 form = new Form1(message); //passing the string
                    }
                    startServer(); //restarts server after passing string
                }
            }
        });
}

这是我在 Form1 中的构造函数:

public Form1(string str)
{
    InitializeComponent();
    MessageBox.Show(str); //message arrives
    addToList(str);
}

这是我的 addToList 方法:

    public void addToList(string str)
    {
        MessageBox.Show(str); //arrives
        textBox1.Text = str; //not arriving/showing in form
    }

我的主要方法:

[STAThread]
static void Main(string[] args)
{
    if (m.WaitOne(TimeSpan.Zero, true))
    {
        startServer();

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());


    }
    else
    {
        startClient("message");
    }

}

Form1 的默认构造函数:

public Form1()
{
    InitializeComponent();
    comboBox1.SelectedIndex = 0;
    comboBox2.SelectedIndex = 1;
    Timer_start();
    checkWindowTimer_start();
    checkBox1.FlatAppearance.BorderSize = 0;
}

我做错了什么?是因为重新初始化组件吗?但是当我不提供 InitializeComponents 时,该工具将不再启动

这感觉很简单,但我现在为此苦苦挣扎了很长一段时间,非常感谢你们的帮助:)

提前致谢 拉旺

最佳答案

从多线程应用程序编辑 UI 时,您可能需要使用 Invoke。您只能从启动它的线程编辑 UI

举个例子:

public partial class Form1 : Form
{
    static Random rand = new Random();

    public Form1()
    {
        InitializeComponent();

        System.Threading.Tasks.Task.Factory.StartNew(changetextfromanotherthread);

    }

    void changetextfromanotherthread()
    {
        for (int x = 0; x < 100; x++)
        {
            Invoke((Action)(() => { textBox1.Text = "" + rand.Next(); }));
            System.Threading.Thread.Sleep(250);
        }

    }
}

这将从另一个线程成功更改文本框 100 次。

如果我们删除调用,我们将在 x == 1 时得到无效操作异常。这表明您不能从另一个线程更改 UI。

但是你不会得到那个错误吧

第二次删除对 Sleep(250) 的调用。现在文本框被更改一次并且没有错误。 (我真的不知道为什么)这与您的问题相似。

无论如何,即使我的某些观点对于此上下文无效,请从 Invoke 中更改文本框。

关于来自构造函数的 C# 字符串未传递给表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26445251/

相关文章:

c# - 限制属性使用在设计时互斥?

c# - AnonymousType 无法序列化。考虑用 DataContractAttribute 属性标记它

c# - 如何判断现在是否是英国夏令时

python - 添加 URL 参数以在 flask 中形成 Action

PHP $_SESSION 数组插入 MYSQL

javascript - 添加所有输入 rel 的总和。适用于复选框,直到添加了选择输入

c# - 在 .NET CF 中以编程方式在 WiFi 网络之间切换?

delphi - 了解构造函数的可见性

java - 了解Java中的深拷贝构造函数

java - getDeclaredConstructors() 列出了 2 个构造函数,但只有一个