gumuruh
gumuruh

Reputation: 2570

Can Multiple BackgroundWorkers use the same Function / end Variable?

Is that okay if let's say I have

BackgroundWoker1
BackgroundWoker2

Both of them are having :

AddHandler BackgroundWoker1.DoWork, AddressOf requestDataTravelPackagesName
AddHandler BackgroundWoker1.ProgressChanged, AddressOf showLoadingAnim

and in the other line (with different time usage)

AddHandler BackgroundWoker2.DoWork, AddressOf requestDataTravelPackagesName
AddHandler BackgroundWoker2.ProgressChanged, AddressOf showLoadingAnim

Is this allowed? Because I tried the similar thing, and it appeared that my Form malfunctions. But it doesn't gives me any error / message. the Frame (GUI) just vanished.

I'm thinking perhaps this is because of the multiple background workers that I have. Probably they are accessing the same variable (at the completed function) or in the similar function / etc.

Upvotes: 0

Views: 1130

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54457

There's absolutely no issue with what you're suggesting in principle. If there's an issue then it's with your implementation.

The DoWork event handler is simply a method that gets executed on a secondary thread. It's very common to have the same method executing on multiple threads at the same time and there's no issue with that. The ProgressChanged event handler gets executed on the UI thread so you're never going to have more than one instance executing at a time anyway.

As in all multi-threading scenarios, you always need to ensure that you synchronise access to common data. All the issues that may arise with single-threaded code must be taken into account too, as well as possible cross-threaded access to controls to avoid.

To know what the issue is in your specific case, we'd need to know EXACTLY what happens in your specific case.

Upvotes: 1

Related Questions