Marcは言ったように、長期間の計算を行うために新しいスレッドをスピンオフする必要があります。そうすれば、完了したパーセンテージを変更するたびに、ユーザーインターフェイスのスレッド(すべての画面更新を行う必要があるスレッド)がプログレスバーを再描画できます。
UIスレッドがインターフェイスを更新できることに注意することが重要です。したがって、別のスレッドで実行した後は、UIの変更がUIスレッド上で処理されるように余分なフープを実行する必要があります。実行中のスレッドが不明な場合は、InvokeRequiredの値を確認して(実際のクラスがSystem.Windows.Formの場合)、UIスレッドであるかどうかを確認できます。
UIスレッドでコマンドを処理するには、Control.Invoke()関数を使用して、作業中のコントロールのUIスレッドで更新が処理されていることを確認します。
下のサンプルコードでは、デリゲート関数の型を作成し、呼び出された関数を事前に宣言しています。私はC#3.5のすばらしい関数を使っていませんでしたが、同じことをするラムバ式。
private void bCreateInvoices_Click(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(CreateInvoices);
worker.RunWorkerAsync(this);
}
// Here is the long running function that needs to update the progress bar
public void CreateInvoices(object sernder, DoWorkEventArgs e)
{
int totalChecked = CountCheckedServiceOrders();
int totalCompleted = 0;
foreach (...data to process...) {
totalCompleted++;
if (InvokeRequired) {
Invoke(new Change(OnChange), "status text",
totalCompleted, totalChecked);
}
}
}
// this code updates the status while a background thread works
private delegate void Change(string status, int complete, int total);
private void OnChange(string status, int complete, int total)
{
if (status == null) {
progressBar.Visible = false;
lStatus.Text = "Task complete";
progressBar.Value = 0;
} else {
progressBar.Visible = true;
progressBar.Minimum = 0;
progressBar.Maximum = total;
progressBar.Value = complete;
lStatus.Text = status;
}
}
いくつかの詳細はMSDN Control.InvokeRequired manual pageとMSDN Control.Invoke manual pageを見てみましょう。
ため
おかげで、私はここに答えることがC#の程度は十分知っているが、Javaで、それは別のスレッドを持っている必要がありますか、他あなたがこれと同じ動作を取得しますスイングはありません。 – Chet