using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(50); // 模拟耗时操作
UpdateProgressBar(i);
}
});
}
private void UpdateProgressBar(int i)
{
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(
new Action<int>(UpdateProgressBar), i
);
}
else
{
progressBar1.Value = i;
}
}
}
}
标签:进度条,int,System,progressBar1,ui,UpdateProgressBar,using,多线程 From: https://www.cnblogs.com/hlm750908/p/18531456