リスト内のすべてのアカウントをループし、プロセスを高速化する方法として各アカウントのタスクを使用してアカウントに何かを行うこのコードがあります。プログラムがこのアクションを完了するたびに、ユーザー インターフェイスで進行状況バーを更新する必要があります。以前は Invoke を使用していましたが、最適なオプションではなく、機能させることができませんでした。バックグラウンドワーカーを使用してこれを実行できることはわかっていますが、これはアプリケーションをマルチスレッド化する最良の方法ではないため、これを使用しました。そして、呼び出しの代わりに、ContinueWith について聞いたことがありますが、動作しているようには見えず、赤い下線だけのエラー メッセージは表示されません。コード:
progressBar.Value = 0
Dim tasks As New List(Of Task)()
For Each account In combos
Dim t As Task = Task.Run(Sub()
While checked = False
If proxies.Count = 0 Then
Exit Sub
'Also can't think of a good way to stop searching through accounts when there are no proxies left in my queue.
End If
Dim proxy As New WebProxy(proxies(0))
proxies.TryDequeue(0)
'Do something
End While
checkedAmount += 1
Dim progress As Integer = ((checkedAmount / combos.Count) * 100)
Task.ContinueWith(progressBar.Value = progress, TaskScheduler.FromCurrentSynchronizationContext()) 'Error here
End Sub)
tasks.Add(t)
Next
Task.WaitAll(tasks.ToArray())
私はまた、後にサブを入れようとしましたが、それは何もしませんでした. 事前に助けてくれてありがとう。
呼び出しで更新を試みました:
Private Delegate Sub UpdateProgressBarDelegate(ByVal progressBarUpdate As ProgressBar, ByVal value As Integer)
Dim checkedAmount As Integer = 0
Dim checked As Boolean = False
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
progressBar.Value = 0
Dim tasks As New List(Of Task)()
For Each account In combos
Dim t As Task = Task.Run(Sub()
While checked = False
proxies.TryDequeue(0)
'do stuff
End While
checkedAmount += 1
Dim progress As Integer = ((checkedAmount / combos.Count) * 100)
If Me.InvokeRequired = True Then
Me.Invoke(New UpdateProgressBarDelegate(AddressOf UpdateProgressBar), progressBar, progress)
Else
UpdateProgressBar(progressBar, progress)
End If
'Task.ContinueWith(progressBar.Value = progress, TaskScheduler.FromCurrentSynchronizationContext())
End Sub)
tasks.Add(t)
Next
Task.WaitAll(tasks.ToArray())
End Sub
Private Sub UpdateProgressBar(ByVal ProgressBarUpdate As ProgressBar, progress As Integer)
progressBar.Value = progress
End Sub
それでもうまくいかないのですが、なぜですか?