方便后面查询,写了个测试demo
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace winform异步
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.button1.Enabled = false;
Test();
this.button1.Enabled = true;
}
public void Test()
{
Task task1 = Task.Run(() =>
{
DoSomeThing();
});
}
private void button2_Click(object sender, EventArgs e)
{
this.label1.Text = "没卡主页面";
}
public void DoSomeThing()
{
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 500; j++)
{
string s = i.ToString();
int a = Convert.ToInt32(s);
this.Invoke(new MethodInvoker(() =>
{
this.label2.Text = a.ToString();
this.label3.Text = (a+10).ToString();
}));
}
}
}
public void DoSomeThing1()
{
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 500; j++)
{
string s = i.ToString();
int a = Convert.ToInt32(s);
this.Invoke(new MethodInvoker(() =>
{
this.label4.Text = a.ToString();
this.label5.Text = (a + 10).ToString();
}));
}
}
}
}
}
页面就是简单的

效果如下

