写入从另一个线程一个TextBox? [英] Writing to a TextBox from another thread?
问题描述
我无法弄清楚如何使一个C#Windows窗体应用程序写入到从一个线程一个文本框。例如,在Program.cs中,我们有标准的main()画的形式为:
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新Form1中());
}
然后我们在Form1.cs中:
公共Form1中()
{
的InitializeComponent(); 新的Thread(SampleFunction)。开始();
}公共静态无效SampleFunction()
{
而(真)
WindowsFormsApplication1.Form1.ActiveForm.Text + =嗨。
}
我要对这个完全错误的?
更新
下面是bendewey提供的工作code样品:</ P>
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
新的Thread(SampleFunction)。开始();
} 公共无效AppendTextBox(字符串值)
{
如果(InvokeRequired)
{
this.Invoke(新动作&LT;串GT;(AppendTextBox),新的对象[] {值});
返回;
}
textBox1.Text + =价值;
} 无效SampleFunction()
{
//获取一个单独的线程执行,
睡觉时//不阻止用户界面
的for(int i = 0;我小于5;我++)
{
AppendTextBox(你好);
Thead.Sleep(1000);
}
}
}
在您的MainForm一个函数来设置文本框的检查InvokeRequired
公共无效AppendTextBox(字符串值)
{
如果(InvokeRequired)
{
this.Invoke(新动作&LT;串GT;(AppendTextBox),新的对象[] {值});
返回;
}
ActiveForm.Text + =价值;
}
虽然在静态方法,你可以不只是打电话。
WindowsFormsApplication1.Form1.AppendTextBox(HI);
你必须有一个静态引用到Form1的地方,但这并不推荐使用或有必要,你可以让你的SampleFunction不是静态的,如果是的话,你可以叫
AppendTextBox(HI);
这将追加一个线程不同的充,如果需要使用Invoke调用get编组到用户界面。
全样本的
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
新的Thread(SampleFunction)。开始();
} 公共无效AppendTextBox(字符串值)
{
如果(InvokeRequired)
{
this.Invoke(新动作&LT;串GT;(AppendTextBox),新的对象[] {值});
返回;
}
textBox1.Text + =价值;
} 无效SampleFunction()
{
//获取一个单独的线程执行,
睡觉时//不阻止用户界面
的for(int i = 0;我小于5;我++)
{
AppendTextBox(你好);
Thead.Sleep(1000);
}
}}
I cannot figure out how to make a C# Windows Form application write to a textbox from a thread. For example in the Program.cs we have the standard main() that draws the form:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Then we have in the Form1.cs:
public Form1()
{
InitializeComponent();
new Thread(SampleFunction).Start();
}
public static void SampleFunction()
{
while(true)
WindowsFormsApplication1.Form1.ActiveForm.Text += "hi. ";
}
Am I going about this completely wrong?
UPDATE
Here is the working code sample provided from bendewey:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new Thread(SampleFunction).Start();
}
public void AppendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTextBox), new object[] {value});
return;
}
textBox1.Text += value;
}
void SampleFunction()
{
// Gets executed on a seperate thread and
// doesn't block the UI while sleeping
for(int i = 0; i<5; i++)
{
AppendTextBox("hi. ");
Thead.Sleep(1000);
}
}
}
On your MainForm make a function to set the textbox the checks the InvokeRequired
public void AppendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTextBox), new object[] {value});
return;
}
ActiveForm.Text += value;
}
although in your static method you can't just call.
WindowsFormsApplication1.Form1.AppendTextBox("hi. ");
you have to have a static reference to the Form1 somewhere, but this isn't really recommended or necessary, can you just make your SampleFunction not static if so then you can just call
AppendTextBox("hi. ");
It will append on a differnt thread and get marshalled to the UI using the Invoke call if required.
Full Sample
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
new Thread(SampleFunction).Start();
}
public void AppendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTextBox), new object[] {value});
return;
}
textBox1.Text += value;
}
void SampleFunction()
{
// Gets executed on a seperate thread and
// doesn't block the UI while sleeping
for(int i = 0; i<5; i++)
{
AppendTextBox("hi. ");
Thead.Sleep(1000);
}
}
}
这篇关于写入从另一个线程一个TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!