接触的不是很深,就写个简易的。
代码非常简单,如下:
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Task_InvokeSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task t1 = new Task(dispNum);
t1.Start();
}
void dispNum()
{
for (int i = 0; i < 10000; i++)
{
richTextBox1.Invoke(new Action(()=> {
richTextBox1.AppendText(i.ToString()+"\r\n");
}));
}
}
}
}
这些代码可以简化一下:
private void button1_Click(object sender, EventArgs e)
{
Task t1 = new Task(()=> {