Reputation: 406
I have a legacy app that is suddenly important. It needs to be faster. There are many pieces of code that would be candidates for parallel processing, such as unzipping large files and downloading multiple files instead of one at a time. It also processes data files, and I think this could be done with a thread per file, but I've had trouble with that.
I see two possible solutions.
Solution 1) Create a task for each _file in FilesToProcess and use Task.Run(Tasks.ToArray()).
Solution 2) Add a handle to the main app that taked in a parameter of string and pass the _fileToProcess. Then process the file normally. In this approach, i'm simply launching a new process of the existing application to process a single file, but I launch as many as I need.
My code isn't thread safe. Will I really have to rewrite the code so that all my sub processes are sync locked or await on any problem area I.E. MethodCall1 passing byref? Is there a simpler solution?
If so, solution 2 seems like it will be a much easier approach, even at the loss of transparency.
Below is a pseudo code example of how it's written now.
Public Sub Main()
Dim Foo, Bar
Dim FilesToProcess = DownloadFiles()
For Each _file as string in FilesToProcess
UnzipFile(_file)
MethodCall1(Foo,Bar)
MethodCall2SqlIdentityInsert()
MethodCall2SqlUpdate()
Next
End Sub
Public Sub MethodCall1(ByRef Foo, ByRef Bar)
Foo = Something
bar = SomethingElse
End Sub
pseudo Code Sol1
Public Sub Main()
Dim tasks As List(Of Task) = New List(Of Task)()
Dim FilesToProcess = DownloadFiles()
For Each _file as string in FilesToProcess
tasks.Add(Task.RunSub()
Dim Foo, Bar
UnzipFile(_file)
MethodCall1(Foo,Bar)
MethodCall2SqlIdentityInsert()
MethodCall2SqlUpdate()
End Sub)
Next
Task.WaitAll(tasks.ToArray())
End Sub
Public Sub MethodCall1(ByRef Foo, ByRef Bar)
Foo = Something
bar = SomethingElse
End Sub
Upvotes: 1
Views: 40
Reputation: 1083
I would prefere Solution 1) IF you can isolate instances to make it thread safe.
That means something like
Public Class IsolatedWorker
private fFileName As String
Private fFoo, fBar As Object
Public Sub New(FileName)
fFileName=FileName
End New
Public Sub Process()
UnzipFile
MethodCall1()
MethodCall2SqlIdentityInsert()
MethodCall2SqlUpdate()
End Sub
Private sub UnzipFile()
End Sub
Private sub MethodCall1()
fFoo = Somehting
fBar = SomethingElse
End Sub
Private sub MethodCall2SqlIdentityInsert()
End Sub
Private sub MethodCall2SqlUpdate()
End Sub
End Class
If you can avoid all global variables and persist your state inside IsolatedWorker, than you'll get it thread safe.
Upvotes: 1