-
Aug 17th, 2018, 11:47 AM
#1
Thread Starter
New Member
Vbnet 2017 problem with background worker during update textbox
Hi all,
i create a class CreaConfigAnalizzaXml
and from this class i started background worker.. it's work fine update without problem progress bar.. but it doesn't update textbox.text... for update textbox i create a delegate...
if i put backgroundworker on main form and started i have no problem..
My question is for update the textbox is necessary start background worker from main form (form1) ?
Below part of my class
Code:
class CreaConfigAnalizzaXml
Public Shared Sub CopiaXmlAnalizza(fileadausare As String)
Try
Logger.LogWrite("Copia xml analizza")
If File.Exists(fileadausare) Then
Dim info As New FileInfo(fileadausare)
File.Copy(fileadausare, "C:\tool\program\MCD_ANALIZZAXML" + info.Name, True)
xmldaaprire = "C:\tool\program\MCD_ANALIZZAXML" + info.Name
Form1.TextBoxFilediConfig.Text = "inzio"
End If
' BottonEsegui.Enabled = False
BackgroundWorkerCreaXmlAnalizza.RunWorkerAsync()
Catch ex As Exception
MessageBox.Show("Eccezione: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
' BottonEsegui.Enabled = True
End Try
End Sub
Private Shared Sub BackgroundWorkerCreaXmlAnalizza_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorkerCreaXmlAnalizza.DoWork
Try
Logger.LogWrite("CreaXml")
Dim i As Integer = 0
Dim CBox_WAY As Boolean
' AddTextInterfaccia("Inzio Creazione Xml")
Form1.ProgressBar1.Minimum = 0
BackgroundWorkerCreaXmlAnalizza.WorkerReportsProgress = True
BackgroundWorkerCreaXmlAnalizza.ReportProgress(0, "Working...")
Dim codiciselezionati = Form1.ListBoxDevice.SelectedItems.Count - 1
For i = 0 To codiciselezionati
If Form1.ListBoxDevice.Items.Item(i).ToString.Equals("GSC01") Then
CBox_WAY = True
Else
ArrayXxml.Add(Form1.ListBoxDevice.Items.Item(i).ToString)
End If
Next
cuenta = cuenta + ArrayXxml.Count
If CBox_WAY = True Then
cuenta = cuenta + 1
i = i + 1
BackgroundWorkerCreaXmlAnalizza.ReportProgress(100 * i / cuenta, "Procesando (" & i & "/" & cuenta & ") elementos...")
ModificaXmlXanalizza("ControllaWay", "True")
ModificaXmlXanalizza("CercaDevice", " ")
End If
If ArrayXxml.Count > 0 Then
cuenta = cuenta + 1
ModificaXmlXanalizza("ControllaAltreDevice", "True")
For Each Str As String In ArrayXxml
i = i + 1
cuenta = cuenta + 1
BackgroundWorkerCreaXmlAnalizza.ReportProgress(100 * i / cuenta, "Procesando (" & i & "/" & cuenta & ") elementos...")
ModificaXmlXanalizza("CercaDevice", Str)
Next
End If
BackgroundWorkerCreaXmlAnalizza.ReportProgress(100, "Completado!")
If ConteggioErroriPerCreaXml = 0 Then
AddTextInterfaccia("prova2")
Else
End If
e.Result = True
Catch ex As Exception
ConteggioErroriPerCreaXml = ConteggioErroriPerCreaXml + 1
Logger.LogWrite("Error nel CreaXmlAnalizza " & ex.Message)
' AddTextInterfaccia("Crezione Xml Non Completata")
e.Result = True
End Try
End Sub
Delegate Sub AddTextDelegateInterfaccia(ByVal testo As String)
Public Shared Sub AddTextInterfaccia(ByVal testo As String)
If Form1.TextBoxFilediConfig.InvokeRequired Then
Form1.TextBoxFilediConfig.Invoke(New AddTextDelegateInterfaccia(AddressOf AddTextInterfaccia), testo)
Else
Form1.TextBoxFilediConfig.Text = testo
End If
End Sub
Last edited by dday9; Aug 17th, 2018 at 03:35 PM.
Reason: Added Code Tags
-
Aug 17th, 2018, 03:37 PM
#2
Re: Vbnet 2017 problem with background worker during update textbox
Without reading your code in detail, the control likely needs to be redrawn. Simply do the following in your AddTextInterfaccia method:
Code:
With Form1.TextBoxFilediConfig
If .InvokeRequired Then
.Invoke(New AddTextDelegateInterfaccia(AddressOf AddTextInterfaccia), testo)
Else
.Text = testo
.Refresh()
End If
End With
-
Aug 17th, 2018, 09:08 PM
#3
Re: Vbnet 2017 problem with background worker during update textbox
Ive only recently started using Background workers. Here is an example of how I would set about this. You maybe able to rig something up in the RunWorkerCompleted event also. Or conditionally do things as per the percentage completed in the ProgressChanged event.
The Form:
Code:
Public Class FormStuff
Private Sub ButtonGo_Click(sender As Object, e As EventArgs) Handles ButtonGo.Click
Dim _BGWClass As New BGWClass(Me)
ProgressBar1.Maximum = 100
_BGWClass.BGW.RunWorkerAsync()
End Sub
End Class
The Class:
Code:
Public Class BGWClass
Public WithEvents BGW As New System.ComponentModel.BackgroundWorker With {.WorkerReportsProgress = True}
Dim _FormStuff As FormStuff
Public Sub New(ByVal FormStuff As FormStuff)
_FormStuff = FormStuff
End Sub
Private Sub BGW_DoWork(ByVal Sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BGW.DoWork
For i = 0 To 100
Threading.Thread.Sleep(1)
BGW.ReportProgress(i, "Sum String")
Next
End Sub
Private Sub BGW_(ByVal Sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW.ProgressChanged
_FormStuff.ProgressBar1.Value = e.ProgressPercentage
_FormStuff.TextBox1.Text = DirectCast(e.UserState, String)
End Sub
End Class
-
Aug 17th, 2018, 10:09 PM
#4
Re: Vbnet 2017 problem with background worker during update textbox
Originally Posted by termi_80
for update textbox i create a delegate...
If you're creating delegates when using a BackgroundWorker then you're doing it wrong. The whole point of a BackgroundWorker is that you DON'T have to worry about delegates and invocations. It's all methods and events, just like you have used a thousand times in single-threaded scenarios. You should follow kpmc's example but you might also like to follow the CodeBank link in my signature below and check out my thread on Using The BackgroundWorker for more examples and information. Basically, if you want to update the UI during the background work then call ReportProgress and handle ProgresChanged. That's not just for updating a ProgressBar; it's for any UI changes. If you're going to use delegates at all then you may as well not use the BackgroundWorker at all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|