从不同的线程和不同的类更新文本框 [英] Update textbox from different thread and different class

查看:74
本文介绍了从不同的线程和不同的类更新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新从不同线程调用的文本框时遇到问题。

我在不同的.cs文件中有两个类(Form1.cs和Class1.cs)。



在Form1.cs中,有按钮和文本框控件。流程如下:

1.点击按钮

2.创建并开始新主题



在Class1.cs中,流程更简单:

1.运行一个函数,将文本写入Form1的文本框控件。



问题:

1.test不会在文本框中打印出来。

2. InvokeRequired总是假的,不知道为什么?



这可能是一个简单而愚蠢的问题,但很长一段时间让我讨厌,请提出建议。

谢谢大家。



两个cs文件发布如下:



Form1.cs

 命名空间 WindowsFormsApplication1 
{
public 部分 Form1:表格
{
public Form1( )
{
InitializeComponent ();
}

private void button1_Click( object sender,EventArgs e)
{
Thread th = new Thread( new ThreadStart(Class1.start));
th.Start();
}

public void UpdateTxt( string str)
{
if (InvokeRequired)
{

.Invoke( new 操作< string>(UpdateTxt), new object [] {str});
return ;
}
textBox1.AppendText(str + \ n);
}
}
}





Class1.cs

< pre lang =cs> namespace WindowsFormsApplication1
{
class Class1
{
public Form1 fm = new Form1();

public static void start()
{
Form1 fs = new Form1();
fs.UpdateTxt( test);
}
}
}

解决方案

您正在创建 new Form1 每次调用 Class1.start()

使用实例代替,在行中的Program.cs

 Application.Run( new  Form1()); 

通常,你会是Class1让我们点击一​​个UpdateText()响应的事件。


你好,我用的是事件并且它有效。

非常感谢!

I have a problem when updating textbox which is called from different thread.
I have two classes in different .cs files (Form1.cs and Class1.cs).

In Form1.cs, there are button and textbox controls. The flow is simply like this:
1. Click button
2. Create and start new thread

In Class1.cs, the flow is even simpler:
1. Run a function which is writing a text into textbox control of Form1.

Problem:
1. "test" won't print out in textbox.
2. InvokeRequired is always false, have no idea why?

This may be a simple and stupid question but annoying me for a long time, please kindly advice.
Thank you all.

Two cs files are posted as follows:

Form1.cs

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

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(Class1.start));
            th.Start();
        }

        public void UpdateTxt(string str)
        {
            if (InvokeRequired)
            {

                this.Invoke(new Action<string>(UpdateTxt), new object[] { str });
                return;
            }
            textBox1.AppendText(str + "\n");
        }
    }
}



Class1.cs

namespace WindowsFormsApplication1
{
    class Class1
    {
        public Form1 fm = new Form1();

        public static void start()
        {
            Form1 fs = new Form1();
            fs.UpdateTxt("test");
        }
    }
}

解决方案

You're creating new Form1's with every call to Class1.start().
Use the instance instead, that is created in Program.cs in the line

Application.Run(new Form1());

Usually, you would Class1 let fire an event that UpdateText() reacts to.


Hi lukeer, as you said, I used event and it worked.
Thanks a lot!


这篇关于从不同的线程和不同的类更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆