|
-
Nov 21st, 2013, 03:48 PM
#1
-
Nov 21st, 2013, 04:14 PM
#2
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.
-
Nov 21st, 2013, 04:37 PM
#3
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.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 21st, 2013, 04:47 PM
#4
Addicted Member
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.
-
Nov 21st, 2013, 04:50 PM
#5
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
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 21st, 2013, 06:38 PM
#6
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
-
Nov 21st, 2013, 06:40 PM
#7
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.
-
Nov 22nd, 2013, 12:05 AM
#8
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.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 22nd, 2013, 01:20 AM
#9
Re: Trying to get the name of specific BackGroundWorker
Thanks everyboy, I used OICD812s solution (I working on a console).
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
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
|