2017-01-19 2 views
0

Autodesk Inventor内に3Dモデルを作成するアプリケーションを作成しました。プロセスの進行状況をユーザーに示すプログレスバーを追加したいと思います。ProgressBarがすべてのステップを視覚的に表示しない

Autodesk Inventorのプロセスが多くのCPUを消費すると、進行状況バーにすべてのステップが表示されず、代わりに(たとえば5ステップ先に)ジャンプしてしまうという問題があります。

Windowsフォームにすべての手順を強制的に表示する方法はありますか?または、この動作は一般的ではありませんか?

Private Sub 
    ' Display the ProgressBar control. 
    pbBuildProgress.Visible = True 
    ' Set Minimum to 1 to represent the first file being copied. 
    pbBuildProgress.Minimum = 1 
    ' Set Maximum to the total number of files to copy. 
    pbBuildProgress.Maximum = BodyComponents.Count 
    ' Set the initial value of the ProgressBar. 
    pbBuildProgress.Value = 1 
    ' Set the Step property to a value of 1 to represent each file being copied. 
    pbBuildProgress.Step = 1 


    ' Start loop trough all body components 
    For i = 0 To BodyComponents.Count - 1 

     ' Some code here that does stuff in Autodesk Inventor 

     ' Perform a step 
     pbBuildProgress.PerformStep() 

    Next 
End Sub 

答えて

0

.PerformStep()の後にプログレスバーまたはフォームを更新してください。しかし、特に最大値が大きい場合は、時間がかかることに注意してください。たとえば、StopWatchで作業し、250ミリ秒ごとにフォームを更新することができます。

+0

あなたは少し説明してもらえますが、さらに?何時間もかかりますか?フォームの更新?複数の(そしておそらく不必要な)アップデートを追加すると、どのように役立つはずですか? –

+0

フォームをペイント/更新するのに多くの時間がかかります。あなたは強制的にではなく、不必要な更新からフォームを防ぐ必要があります;-)例えばStopWatchの使用を試し、250msごとに(おそらくApplication.DoEventsで)あなたの更新だけをしてください。これにより、アプリケーションのフリーズを防ぐことができます。 – muffi

+1

「ストップウォッチ」は何かがかかる時間を測定するためのもので、_xミリ秒ごとにコードを実行するのではありません。そして、あなたは '** Application.DoEvents()'を使うべきではありません!そうすることは、ほとんど常に悪いことです。参照:[** UIの応答性とApplication.DoEvents **の危険性を維持する](https://blogs.msdn.microsoft.com/jfoscoding/2005/08/06/keeping-your-ui-responsive-and-アプリケーションの危険性/)。 –

1

あなたは進捗状況を報告するBackgroundWokerを使用してになります。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    ProgressBar1.Minimum = 0 
    ProgressBar1.Maximum = 10 

    BackgroundWorker1.WorkerReportsProgress = True 
    BackgroundWorker1.RunWorkerAsync() 

End Sub 

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 

    For i = 0 To 10 

     Debug.Write(i) 

     BackgroundWorker1.ReportProgress(i) 

    Next 

End Sub 

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged 

    ProgressBar1.Value = e.ProgressPercentage 

End Sub 

をそれはより多くの経験を持った人が、私は事を学ぶことよりも幸せだろう任意の入力を持っている場合、私が今までそうする必要があるものではありませんまたは2つ。

0

一般的な原因は、UIスレッドがあまりにも頻繁に処理できることです。私は10億回を反復処理操作を実行するだけでUIの更新ごとに千万の反復または1%を下回る送信このため

簡単な修正は、クオンタイズあなた割合の進行状況にあります。

コード:

var steps = 1000000000; 
var step = 1.0d/steps; 
var percent = 0; 
for (var i = 0; i < steps; i++) 
{ 
    var percent1 = (int) (Math.Floor(i * step * 100)); 
    if (percent1 > percent) 
    { 
     // TODO invoke your UI progress bar update here 
     Console.WriteLine("Updating: percent = {0}, i = {1}", percent1, i); 
     percent = percent1; 
    } 
} 

結果:

Updating: percent = 1, i = 10000000 
Updating: percent = 2, i = 20000000 
Updating: percent = 3, i = 30000000 
Updating: percent = 4, i = 40000000 
Updating: percent = 5, i = 50000000 
Updating: percent = 6, i = 60000000 
Updating: percent = 7, i = 70000000 
Updating: percent = 8, i = 80000000 
Updating: percent = 9, i = 90000000 
Updating: percent = 10, i = 100000000 
Updating: percent = 11, i = 110000000 
Updating: percent = 12, i = 120000000 
Updating: percent = 13, i = 130000000 
Updating: percent = 14, i = 140000000 
Updating: percent = 15, i = 150000000 
Updating: percent = 16, i = 160000000 
Updating: percent = 17, i = 170000000 
Updating: percent = 18, i = 180000000 
Updating: percent = 19, i = 190000000 
Updating: percent = 20, i = 200000000 

あなたはまた、非同期で、このような操作を実行することをお勧めします

Task-based Asynchronous Programming

関連する問題