2015-02-09 78 views
-4

如何在顯示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,所以沒有必要報告進度。

+0

其實很容易做到..perhaps你應該做一個谷歌搜索如何創建一個啓動畫面和或顯示一個窗體前顯示一個進度欄.. – MethodMan 2015-02-09 20:52:01

+0

這將有助於看到更多的細節 - 確切地說,你被困住,哪部分你需要幫助,什麼錯誤你會得到(如果有的話)。 – 2015-02-09 21:06:04

+0

@GrantWinney如果我使用RetornoManifestacao.Show(),就像上面那樣,表單會在threadProcesso結束之前出現。我需要它在Thread結束後纔出現。 – 2015-02-09 21:43:07

回答

1

您只需將呼叫移至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

相關問題