官方文件 Code 整理,就不直接操作 thread 囉
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ThreadSafeSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
WriteTextSafe("This text was set safely.");
});
}
public void WriteTextSafe(string text)
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new Action(() => WriteTextSafe(text)));
}
else
{
textBox1.Text = text;
}
}
}
}
沒有留言:
張貼留言