如何在显示ProgressBar时执行方法,然后在显示Form后执行?
问题描述:
如何在显示ProgressBar的同时执行方法,然后显示Form?如何在显示ProgressBar时执行方法,然后在显示Form后执行?
private void btnCienciaEmissao_Click(object sender, EventArgs e)
{
var progressForm = new ProgressBar.frmPBText();
var retornoManifestacao = new frmConsultaNotaEmitidaContraCNPJAntigoRetornoManifestacao();
var threadProcesso = new Thread(() =>
{
Parametros.DgvRetornoManifestacao = ExecutaManifestacao();
progressForm.BeginInvoke(new Action(() => { progressForm.Close(); }));
retornoManifestacao.BeginInvoke(new Action(() => { retornoManifestacao.dgvRetornoManifestacaoDataSource(Parametros.DgvRetornoManifestacao);}));
});
threadProcesso.Start();
progressForm.Show();
// I WANT TO SHOW RetornoManifestacao ONLY AFTER threadProcesso FINISHED
retornoManifestacao.Show();
}
我希望表单retornoManifestacao
显示threadProcesso
结束后。
如果我使用retornoManifestacao.Show()
,就像上面那样,表格会出现在threadProcesso
之前。我需要它在Thread结束后才出现。
我尝试使用threadProcesso.Join()
,但progressForm
冻结。
我的progressForm
有一个选取框样式ProgressBar
,所以没有必要报告进度。
答
您只需将呼叫移至Show()
到您的线程中,以便它作为线程的最后一件事情执行。例如:
private void btnCienciaEmissao_Click(object sender, EventArgs e)
{
var progressForm = new ProgressBar.frmPBText();
var retornoManifestacao = new frmConsultaNotaEmitidaContraCNPJAntigoRetornoManifestacao();
var threadProcesso = new Thread(() =>
{
Parametros.DgvRetornoManifestacao = ExecutaManifestacao();
progressForm.BeginInvoke((MethodInvoker)(() =>
{
// These can (and should) all go in a single invoked method
progressForm.Close();
retornoManifestacao.dgvRetornoManifestacaoDataSource(Parametros.DgvRetornoManifestacao);
retornoManifestacao.Show();
}));
});
threadProcesso.Start();
progressForm.Show();
}
这就是说,如果你使用的是.NET 4.5,在我看来有所不同的方法整体能更好地工作:
private async void btnCienciaEmissao_Click(object sender, EventArgs e)
{
var progressForm = new ProgressBar.frmPBText();
progressForm.Show();
Parametros.DgvRetornoManifestacao = await Task.Run(() => ExecutaManifestacao());
progressForm.Close();
var retornoManifestacao = new frmConsultaNotaEmitidaContraCNPJAntigoRetornoManifestacao();
retornoManifestacao.dgvRetornoManifestacaoDataSource(Parametros.DgvRetornoManifestacao);
retornoManifestacao.Show();
}
+0
**谢谢,它的工作!** – 2015-02-10 11:55:12
其实很容易做到..perhaps你应该做一个谷歌搜索如何创建一个启动画面和或显示一个窗体前显示一个进度栏.. – MethodMan 2015-02-09 20:52:01
这将有助于看到更多的细节 - 确切地说,你被困住,哪部分你需要帮助,什么错误你会得到(如果有的话)。 – 2015-02-09 21:06:04
@GrantWinney如果我使用RetornoManifestacao.Show(),就像上面那样,表单会在threadProcesso结束之前出现。我需要它在Thread结束后才出现。 – 2015-02-09 21:43:07