Fred Kerber
Fred Kerber

Reputation: 187

Can Background workers share functions

I have a rather complicated window form application that is managing 2 background worker threads. Everything is running as expected but I find myself writing things twice, once for each background worker and I'm not sure if this is required. For example, I have in one of the Do_Work routines:

Do While ThisQueue.FIleList.Count > 0
  ..Do some work..
  Call Worker1DoSomething(SomeVar)
  ..Do more work..
Loop

In the second background worker's Do_Work routine I have:

Do While ThisQueue.FileList.Count > 0
  ..Do some work..
  Call Worker2DoSomething(SomeVar)
  ..Do more work..
Loop

Can I Have 1 function called by both Do_Work routines and not get things messed up? Instead of having two functions: Worker1DoSomething(byval thisvar as string) and Worker2DoSometing(byval thisvar as string) just have 1 function WorkerFunctionDoSomething(byval thisvar as string) ?

The biggest reason why I ask is that I have 8 functions that are really 4 functions that are called from either Do_Work routine and it would simplify the code big time. One of the functions writes data to a SQL server, which was the main reason why I started out going this method.

Thanks in advance for any help or suggestions! Fred

Upvotes: 1

Views: 65

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54457

You certainly can do that. Like any event handler, inside the DoWork event handler, the sender parameter is the object that raised the event. If you set up a relationship between each BackgroundWorker and the data you want it to use and/or the method(s) you want to execute, you can use the sender to get that data and/or method(s) and make use of them. One way to do that is with a Dictionary with the BackgroundWorker objects as keys:

Private ReadOnly workMethodsByWorker As New Dictionary(Of BackgroundWorker, Action(Of Object)) From {{BackgroundWorker1, AddressOf Method1},
                                                                                                     {BackgroundWorker2, AddressOf Method2}}

Private data As Object

Private Sub BackgroundWorkers_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork,
                                                                                     BackgroundWorker2.DoWork
    Dim worker = DirectCast(sender, BackgroundWorker)
    Dim method = workMethodsByWorker(worker)

    method(data)
End Sub

Private Sub Method1(data As Object)
    '...
End Sub

Private Sub Method2(data As Object)
    '...
End Sub

Another option is to define your own class that inherits BackgroundWorker and adds a property for the method to be executed and use that class instead of the standard BackgroundWorker:

Imports System.ComponentModel

Public Class BackgroundWorkerEx
    Inherits BackgroundWorker

    Public Property WorkMethod As Action(Of Object)

End Class
Private data As Object

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorkerEx1.WorkMethod = AddressOf Method1
    BackgroundWorkerEx2.WorkMethod = AddressOf Method2
End Sub

Private Sub BackgroundWorkers_DoWork(sender As Object, e As DoWorkEventArgs)
    Dim worker = DirectCast(sender, BackgroundWorkerEx)
    Dim method = worker.WorkMethod

    method(data)
End Sub

Private Sub Method1(data As Object)
    '...
End Sub

Private Sub Method2(data As Object)
    '...
End Sub

Upvotes: 1

Related Questions