almamater Posted March 30, 2012 at 09:12 AM Report #446537 Posted March 30, 2012 at 09:12 AM Visual Basic 2010 Olá a todos, Estava a tentar utilizar um backgroundworker para atualizar uma progressbar que não consigo atualizar até que determinados processos terminem.. ou seja: Por exemplo neste bocado de código a minha progressbar não atualiza logo ao longo dos processo: Progressbar1.Value = 30 Application.DoEvents() If clsSerial.IsOpen Then ListBox1.Items.Clear() Me.Cursor = Cursors.WaitCursor clsSerial.Discard() If RadioButton1.Checked = True Then clsSerial.Send("Ga" & CbFanModo.SelectedIndex & "S") System.Threading.Thread.Sleep(1000) clsSerial.Send("Gb" & CbFanVar.Text & "S") System.Threading.Thread.Sleep(1000) clsSerial.Send("Gc" & CboxFase1.Text & "S") System.Threading.Thread.Sleep(1000) clsSerial.Send("Gd" & CboxFase2.Text & "S") System.Threading.Thread.Sleep(1000) clsSerial.Send("Ge" & CboxFase3.Text & "S") System.Threading.Thread.Sleep(1000) If xModo = 0 Then clsSerial.Send("Gf" & CKboxFan.CheckState & "S") System.Threading.Thread.Sleep(1000) Progressbar1.Value = 60 Application.DoEvents() End If If xModo = 1 Then clsSerial.Send("GF" & CKboxFan.CheckState & "S") System.Threading.Thread.Sleep(1000) Progressbar1.Value = 60 Application.DoEvents() End If End If Testei adicionar um backgroundworker da seguinte forma: Dim Pbar1Value As Integer = 0 Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _ ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker) worker.ReportProgress(Pbar1Value, Pbar1Value) End Sub Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _ ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged Me.Progressbar1.Value = e.ProgressPercentage End Sub Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _ ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 'NÃO COLOQUEI AQUI NADA End Sub Depois sempre que quero atualizar a progressbar fiz assim: Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Pbar2Value = 15 Me.BackgroundWorker1.RunWorkerAsync() (...) código Pbar1Value = 30 Me.BackgroundWorker1.RunWorkerAsync() End Sub Mas ocorre o seguinte erro: "This BackgroundWorker is currently busy and cannot run multiple tasks concurrently." Isto ocorre porque no mesmo processo eu quero alterar o valor da progressbar mais do que uma vez e preciso de colocar o "BackgroundWorker1.RunWorkerAsync()" várias vezes.. mas não consigo.
Caça Posted March 30, 2012 at 09:22 AM Report #446539 Posted March 30, 2012 at 09:22 AM Vê isto: https://wiki.portugal-a-programar.pt/dev_net/vb.net/backgroundworkers/ Pedro Martins Não respondo a duvidas por PM
almamater Posted March 30, 2012 at 09:38 AM Author Report #446543 Posted March 30, 2012 at 09:38 AM Pois.. eu acho que para fazer o que quero tenho de colocar o código todo que quero executar dentro do BGW_DoWork eu apenas estava a usar o BGW_DoWork para atualizar a progressbar mas não fica funcional 😕 vou alterar umas coisas então..
ribeiro55 Posted March 30, 2012 at 09:55 AM Report #446545 Posted March 30, 2012 at 09:55 AM Somente o que está dentro do DoWork é que é executado em thread diferente 😕 Tem de ser precisamente ao contrário. O trabalho é para o worker, a actualização da progress bar é fora. O worker tem técnicas de relatar progresso, como podes ver no artigo na Wiki Sérgio Ribeiro "Great coders aren't born. They're compiled and released""Expert coders do not need a keyboard. They just throw magnets at the RAM chips"
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now