Results 1 to 9 of 9

Thread: [RESOLVED] Trying to get the name of specific BackGroundWorker

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [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?
    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!

  2. #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.

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  4. #4
    Addicted Member
    Join Date
    Oct 2012
    Location
    Springfield, IL
    Posts
    142

    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:
    1. '...
    2.     Dim WithEvents worker1 As New BackgroundWorker
    3.     Dim WithEvents worker2 As New BackgroundWorker
    4.     '...
    5.     Private Sub BackgroundWorker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) '...
    6.         Dim workerName As String
    7.         If sender Is worker1 Then
    8.             workerName = "Worker 1"
    9.         ElseIf sender Is worker2 Then
    10.             workerName = "Worker 2"
    11.         End If
    12.         ...
    13.     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.

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    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:
    1. Imports System.Net
    2.  
    3. Public Class MainForm
    4.  
    5.     Private ReadOnly temp As String = "http://download.piriform.com/ccsetup327.exe"
    6.  
    7.     Private m_dictionary As New Dictionary(Of WebClient, ListViewItem)
    8.  
    9.  
    10.     Private Sub Download(ByVal Path As String, ByVal FileName As String)
    11.         Dim client As New WebClient
    12.         AddHandler client.DownloadProgressChanged, AddressOf DownloadProgressChanged
    13.         AddHandler client.DownloadFileCompleted, AddressOf DownloadFileCompleted
    14.  
    15.         Dim lvw As New ListViewItem With {.Text = FileName}
    16.         lvw.SubItems.Add("0%")
    17.         Me.FileDownLoadListView.Items.Add(lvw)
    18.  
    19.         client.DownloadFileAsync(New Uri(temp), Path & Filename)
    20.         Me.m_dictionary.Add(client, lvw)
    21.     End Sub
    22.  
    23.     Private Sub DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
    24.         m_dictionary.Item(CType(sender, WebClient)).SubItems(1).Text = e.ProgressPercentage & "%"
    25.     End Sub
    26.  
    27.     Private Sub DownloadFileCompleted(ByVal sender As Object, _
    28.                                                        ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
    29.         Dim client As WebClient = DirectCast(sender, WebClient)
    30.         client.Dispose()
    31.         m_dictionary.Remove(client)
    32.         Debug.WriteLine(m_dictionary.Count)
    33.     End Sub
    34.  
    35.     Private Sub StartButton_Click(ByVal sender As System.Object,
    36.                                   ByVal e As System.EventArgs) Handles StartButton.Click
    37.         For i As Integer = 0 To 5
    38.             Download("C:\Temp\", String.Format("FileName{0}.exe", i))
    39.         Next
    40.     End Sub
    41. End Class
    My Github - 1d3nt

  7. #7
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  8. #8

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  9. #9

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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
  •  



Click Here to Expand Forum to Full Width