事情正在冻结正是因为你没有“一次运行多个事情”。
您正在事件处理程序中为单击按钮执行长时间运行的操作。在该事件处理程序返回之前,UI 将被阻止。
尝试将长时间运行的进程放在另一个线程中,例如使用Task或BackgroundWorker。
另一个线程可以更新进度条。但是,请记住,单独的线程需要正确访问 UI 线程。确切的机制取决于您是否在谈论 WinForms、WPF 或其他东西(未在您的问题中指定)。
这是我最喜欢的从 WinForms 的非 UI 线程更新控件的方法:
https://stackoverflow.com/a/3588137/141172
更新
这是一个例子。我手边没有 IDE,所以可能会有一些小问题。
private BackgroundWorker worker = new BackgroundWorker();
public MyForm() // Your form's constructor
{
InitializeComponent();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
}
private void btnUpload_Click(object sender, EventArgs e)
{
if (!worker.IsBusy) // Don't start it again if already running
{
// Start the asynchronous operation.
// Maybe also disable the button that starts background work (btnUpload)
worker.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
worker.ReportProgress(10);
RunLongProcess();
worker.ReportProgress(20);
RunAnotherLongProcess();
worker.ReportProgress(50);
RunOneMoreLongProcess();
worker.ReportProgress(100);
}
// This event handler updates the progress.
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressbar.value = e.ProgressPercentage;
}
// This event handler deals with the results of the background operation.
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Inform the user that work is complete.
// Maybe re-enable the button that starts the background worker
}