Avatar of sirbounty
sirbountyFlag for United States of America asked on

more bgw help

When my 'update' button is clicked, I'd like to run two simultaneous SQL update calls (to separate fields).
I have the following code, but (1) want to shorten it, and (2) want to include an overall 'if both are done then...' section.

I placed a class-level boolean thinking I could do it that route, but now I'm second-guessing myself...

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
 Dim bgwE, bgwLB As New BackgroundWorker
 AddHandler bgwE.DoWork, AddressOf bgwEWork
 AddHandler bgwE.RunWorkerCompleted, AddressOf bgwEDone
 AddHandler bgwLB.DoWork, AddressOf bgwLBWork
 AddHandler bgwLB.RunWorkerCompleted, AddressOf bgwLBDone
 bgwE.RunWorkerAsync()
 bgwLB.RunWorkerAsync()
End Sub

Open in new window

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
sirbounty

8/22/2022 - Mon
Sancler

To make the code here shorter, I'd drag two BGWs onto my Form - is it a Form app? ;-) - and just code straight into the subs that intellisense then provides.

On the "if both are done then ..." issue, would this do?

        If bgwE.IsBusy OrElse bgwLB.IsBusy Then

Roger
ASKER
sirbounty

Aha - had looked at isbusy - but didn't hit me to use them both in conjunction.
It's a form app - but I'm curious how that would make it shorter?  Wouldn't it be about the same?  
I was thinking more along the lines of runasync with a parameter to call the appropriate code...what do you think?
Sancler

It only makes it shorter in the sense that the declaration, initialisation and AddHandler is, in effect, done once at design time, rather than being done afresh in code every time you call the button_click sub.

Yes, you can call bgw.RunWorkerAsync with an argument.  But is it appropriate here?  If you want to set two processes running at the same time, you're going to need two BGWs anyway.  Or are you thinking you could use just one, letting it finish the first task before starting it on the second?  In that case, one could be used with different arguments on the two calls.  Or do you want each to run its own SQL statement but with possibly different SQL parameters each time?  Again, then, the argument would be useful.

But, on my original impression from the code you posted, I don't see the upside of it.

BTW, I think you already know but I'm in the UK.  So I'm off to bed in a few moments.  So a few hours' silence from this end doesn't mean I'm ignoring you ;-)

Roger
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
sirbounty

Yep - I remembered - and that's okay.  This is for a routine to sync two fields in the same table.  Perhaps I might be able to reduce the routine to one instead of trying to figure this out - let SQL do the work. :^)

Good info and I'm not working on this again until I start work again tomorrow.
ASKER
sirbounty

The isbusy is throwing an error because the 'other one' has a value of nothing (because it's already done).

I'm looking at doing something like this: (may also run another routine after setting the label...)
Private Sub bgwEDone(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
'do nothing - this one 'should' finish first...??
End Sub
 
Private Sub bgwLBDone(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
  If bgwE is done and bgwlb is now done, then...
    Label2.Text = dgv1.RowCount & " records (updated)."
  End If
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Sancler

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
sirbounty

Sorry I don't get notifs at work, so it takes me a bit to check the updates.
I had since implemented:
If bgwE is nothing andalso bgwLB is nothing then
'  set label text
End if

and placed this inside the second 'done'.
Appears that the bgw is 'nothing' once it hits the done.
Unless you see a blatantly obvious problem with that route - I think I'm all set...?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sancler

Don't see anything "blatantly" obvious.  Suck it and see ;-)

Roger
ASKER
sirbounty

Always appreciated Roger - thanx! :^)
ASKER
sirbounty

Hi Roger - if you're available to help - I'm trying my hand at another background worker over at https://www.experts-exchange.com/.Q_24365554.html 
I don't know if it's stalling because I explained it improperly, but basically I have a module with the bgw that scrapes web data.  I simply want the form's progress bar to display that 'something' is going on...  I appreciate any assistance you can offer...
Your help has saved me hundreds of hours of internet surfing.
fblack61
Sancler

I see that Fernando's sorted this out for you.  I haven't tested his code, but it looks OK to me.

I think the "hanging" problem (and maybe the double counting) arose from a conflict between the simultaneous use of bgw.IsBusy and bgw.ReportProgress.  Certainly, if I commented out lines 15 to 19 in the code in post #24265021 it was working OK for me.  And I don't think the correction on that point arose from any of the other (minor) changes I made to get it to run in my environment.

Roger
ASKER
sirbounty

Great - I appreciate your feedback.
Thanx much. :^)