[RESOLVED] Trying to get the name of specific BackGroundWorker
I'm using several BackgroundWorkers, all of them share the same _DoWork _ProgressChanged and _RunWorkerCompleted Subs.
For the _DoWork and _RunWorkerCompleted Subs it migth be possible the put that info into the Object the is handed over as .Argument and or .Result. I'd like the give a feedback which Backgroundworker is reporting a progress, but I can't find a way the give the name back.
Would anybody have an idea?
Re: Trying to get the name of specific BackGroundWorker
In your signature, there is an object called "sender". Cast that to a BackgroundWorker and you have the worker that reported.
Re: Trying to get the name of specific BackGroundWorker
Thanks for that, however I looking for a way to get the name of the worker. I that there isn't a property .name, but there has to be way. I just want to able to show (on the console or a label)which of my workers reported that progress.
Re: Trying to get the name of specific BackGroundWorker
The easiest way is to test which backgroundworker object is passed in the event:
vb.net Code:
'...
Dim WithEvents worker1 As New BackgroundWorker
Dim WithEvents worker2 As New BackgroundWorker
'...
Private Sub BackgroundWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) '...
Dim workerName As String
If sender Is worker1 Then
workerName = "Worker 1"
ElseIf sender Is worker2 Then
workerName = "Worker 2"
End If
...
End Sub
But that seems too obvious of an answer so maybe its not the answer you're looking for.
An other way would be to inherit the BackGroundWorker and add a Name property. Yet another way would be if the bgWorker was add as a component in the designer, then there should be a way to work backwards through the items added to the form and get the "(Name)" property that is visible in the Property Grid...but that seems like a lot of work.
Re: Trying to get the name of specific BackGroundWorker
That would work? I can't test it right now, will do it first think tomorrow mornung. Thanks
Re: Trying to get the name of specific BackGroundWorker
They are not using the same events though. Here is an example using multiple clients
vb Code:
Imports System.Net
Public Class MainForm
Private ReadOnly temp As String = "http://download.piriform.com/ccsetup327.exe"
Private m_dictionary As New Dictionary(Of WebClient, ListViewItem)
Private Sub Download(ByVal Path As String, ByVal FileName As String)
Dim client As New WebClient
AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressChanged
AddHandler client.DownloadFileCompleted, AddressOf DownloadFileCompleted
Dim lvw As New ListViewItem With {.Text = FileName}
lvw.SubItems.Add("0%")
Me.FileDownLoadListView.Items.Add(lvw)
client.DownloadFileAsync(New Uri(temp), Path & Filename)
Me.m_dictionary.Add(client, lvw)
End Sub
Private Sub DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
m_dictionary.Item(CType(sender, WebClient)).SubItems(1).Text = e.ProgressPercentage & "%"
End Sub
Private Sub DownloadFileCompleted(ByVal sender As Object, _
ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
Dim client As WebClient = DirectCast(sender, WebClient)
client.Dispose()
m_dictionary.Remove(client)
Debug.WriteLine(m_dictionary.Count)
End Sub
Private Sub StartButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles StartButton.Click
For i As Integer = 0 To 5
Download("C:\Temp\", String.Format("FileName{0}.exe", i))
Next
End Sub
End Class
Re: Trying to get the name of specific BackGroundWorker
I'm slightly concerned that you have multiple BGWs that you want to show by "name" in the UI. My guess is that you're conflating the idea of "a set of work" (of which you have several) and the mechanism by which that work is done asynchronously. You shouldn't need more than one BGW. If you think that you do, you probably would be better served by a different mechanism.
Re: Trying to get the name of specific BackGroundWorker
I have a "lengthy work" where a specific check has to be done a great number items. I split up this number of items and let each worker do its own bunch of items. That already works except for the report.
Re: Trying to get the name of specific BackGroundWorker
Thanks everyboy, I used OICD812s solution (I working on a console).