Results 1 to 24 of 24

Thread: [RESOLVED] How to pause a loop until a variable gets set, or an event gets raised?

  1. #1

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Resolved [RESOLVED] How to pause a loop until a variable gets set, or an event gets raised?

    I have a loop to download multiple reports. The browser is CefSharp and a download handler raises an event when the download has finished. Because of file naming, i only want one download at a time, so the loop needs to pause while waiting for the report to download.

    For example, here is some pseudocode (i hope that's okay):

    Code:
    Sub Download_Reports()
    
      For Each Report In Reports
    
         Setup_Report()
    
         Generate_Report()
    
         Wait until download finishes.
    
      Next
    
    End Sub
    I could wait with a loop that includes Application.DoEvents every decisecond or so, checking some variable that gets set by the event. But i was thinking if i have an event, maybe there's another solution. What do you think the best way to approach this is?

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

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    A webbrowser is a UI object. If it is not being used as such then you should not be using it. If you are downloading files look at the webclient class or if you need more control the httwebrequest classes. Do not ever use Application.DoEvents.

  3. #3

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    If it is not being used as such then you should not be using it.
    I disagree. I need to login, use javascript to modify the html, and capture the generated reports as they appear. I was doing it with Selenium and Titanium proxy, but as it began to fail, i have switched to CefSharp. The browser will most likely be hidden most of the time, but showing it will allow faster debugging of errors which popup and are usually visible.

    The issue here is that i need to wait after each download because there is only one download handler to name the file. (I asked in the CefSharp gitter chat, and this is what was recommended.) This then requires a pause, perhaps akin to awaiting an async function.

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

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Disagree with it all you like. You are abusing a UI element. Webbrowser render UI elements. You are rendering these elements hidden. This is abuse. Use the correct classes to obtain your data.

  5. #5

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    I hear your point, and it is a good one. Rightly or wrongly, i feel you don't understand what CefSharp is or why i am using it.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by chacham View Post
    The browser is CefSharp and a download handler raises an event when the download has finished.
    The whole point of an event is that it notifies you when something has happened so that you can react to it. The solution to your problem is to NOT use a loop in the first place. You setup and generate the first report and then you're done. When the event is raised, you setup and generate the next report and then you're done. Etc, etc, until there are no more reports. You can add the contents of Reports to a Queue or a Stack and that will allow you to take one item at a time until there are none left, e.g.
    vb.net Code:
    1. Private itemsToProcess As Queue(Of Object)
    2.  
    3. Private Sub StartProcessing()
    4.     Dim items = {"First", "Second", "Third"}
    5.  
    6.     itemsToProcess = New Queue(Of Object)(items)
    7.     ProcessNextItem()
    8. End Sub
    9.  
    10. Private Sub ProcessNextItem()
    11.     If itemsToProcess.Any() Then
    12.         Dim item = itemsToProcess.Dequeue()
    13.  
    14.         'Process item here.
    15.     End If
    16. End Sub
    17.  
    18. Private Sub SomeObject_SomeEvent(sender As Object, e As EventArgs) Handles SomeObject.SomeEvent
    19.     'Finish processing previous item here if required.
    20.  
    21.     ProcessNextItem()
    22. End Sub

  7. #7

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Ah, excellent, that sounds like a good idea. Thank you.

    The old code used a loop so now i just have to rewrite that part, changing the For Each into a queue. That might even make the code clearer, as remembering the state of a loop can get confusing. Since there is only one type of download that i am capturing (the pdf) i can use the event to trigger the next cycle, just like you explained. This sounds perfect, and that's a great piece of sample code.

    Fwiw, i'm the one raising the event as part of the implementation of the download handler. I want the code for the handler to be reusable, hopefully in a dll. Thank you for the quick reply.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    One thing that you should probably confirm is whether the event is raised on the UI thread or a secondary thread and whether that makes a difference. If you need to access the UI in code called from the event handler then it matters, otherwise it doesn't. I have no personal experience with that event so I don't know either way.

  9. #9

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    I was able to capture the event in the main thread during a quick test. I'm going to keep that in mind just in case.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    By the way, looping around a DoEvents is almost always a bad idea. And for those rare cases that it is good...it's still very bad. If it ever looks like the right way to go, then that's a pretty clear indication that something is wrong.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by Shaggy Hiker View Post
    By the way, looping around a DoEvents is almost always a bad idea. And for those rare cases that it is good...it's still very bad. If it ever looks like the right way to go, then that's a pretty clear indication that something is wrong.
    And that's why i asked.

    I would suggest though, instead of saying something is wrong, show a better way of doing it, ideally together with a simple example like jmcilhinney gave above. It's the super simple examples that i learn the most from, as it allows me to grasp the concept easily. In this case, i knew i could use an event--which is why i asked--i just hadn't realized how, because i was caught up thinking about a loop. jmcilhinney pointed out the mistake, gave another way to look at it, and provided a simple example that illustrated the point. Easy to grasp and understand, i hope to implement it this afternoon.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by chacham View Post
    And that's why i asked.

    I would suggest though, instead of saying something is wrong, show a better way of doing it, ideally together with a simple example like jmcilhinney gave above. It's the super simple examples that i learn the most from, as it allows me to grasp the concept easily. In this case, i knew i could use an event--which is why i asked--i just hadn't realized how, because i was caught up thinking about a loop. jmcilhinney pointed out the mistake, gave another way to look at it, and provided a simple example that illustrated the point. Easy to grasp and understand, i hope to implement it this afternoon.
    I'm not sure that it's really for you to tell a moderator who's been doing this for 18 years what they should and shouldn't be including in their posts. It's somewhat amusing that you hold me up as an example of what should be done when I'm the one usually getting criticised for not writing people's code for them so they don't have to use a search engine. Just assume that we will post what we see fit to post and if it helps you then that's great and if it doesn't then it doesn't. Also keep in mind, though, that those of us who have been doing this a long time don't generally post anything without a purpose. That means that, even if you don't see it, there's a fair chance that it will help you if you let it. In this case, Shaggy was presumably not trying to provide a solution because he knew that you already had one. He was just posting a an ancillary bit of information that could be useful to you in the future if you heed it. Not everything has to be just what you want because you don't always know what will be useful.

  13. #13

    Thread Starter
    Junior Member chacham's Avatar
    Join Date
    Jun 2020
    Posts
    20

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    In this case, Shaggy was presumably not trying to provide a solution because he knew that you already had one. He was just posting a an ancillary bit of information that could be useful to you in the future if you heed it.
    Yeah, i know. But here's the problem as i see it. This is not to say i'm right, it's just to give you my mindset when reply in this particular thread. (I have a couple other threads here that are fine.)

    I posted a question mentioning a problem, saying how it could be done, and a realization that it could probably be better. The first person responded by saying i was doing it wrong. I normally ignore snarky answers like that, but i was afraid that others would see a response and ignore the question. So, i explained why this case was not what he was thinking, which was presumably ignored as he then challenged my usage of vb in this case. I responded with my opinion, and luckily you replied after that.

    You then gave a great answer, though by bolding the word "not" i felt it was given with a little heat. I ignored that, read the answer twice, and thanked you while showing you that i understood the answer and planned to use it. Great!

    Then another person comes along and decides to add his opinion. There was no reason for the reply, but even with it, the first sentence was good enough. Going on sounds like a complaint, and that is generally not a good way of teaching people how to do things. I felt like he missed the very reason why i asked the question, which was because i was looking for a better way to do it. At this point, i felt it was getting ridiculous. It's as if they just wanted to throw a pie in my face. So, i decided to praise your answer a little more and explain that is how things are best taught to me.

    After writing the above i realize it was a mistake to reply in the first place. I should have let that first answer slide and hoped for the best. I apologize if i offended anyone.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    It's called emphasis. By drawing someone's attention to a specific word or repeating something in different ways you reinforce that it's important. We don't know what you'll take seriously or what you you'll forget the moment you leave this thread. We're doing what we can within this medium to try to ensure that you absorb the most important stuff and benefit from it now and in the future. So many beginners seem so touchy. You are far from the worst in that regard. You should assume that not everyone is going to take care that their post sounds as nice as possible when read in someone else's head. Some of us just want to convey information. We generally won't try to offend but if something's wrong then we'll say it's wrong without a particular effort to spare your feelings. If that's important to you then you probably ought not to rely on strangers who aren't being paid.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Well, it was a complaint. A bit of a rant, even. By now I probably have a history of it when it comes to a loop around a DoEvents. There's a thread over in the General Developer forum where I measured the power consumption of a computer while doing such a busy wait, which is what that is called. People can have good ideas, bad ideas, semi-good ideas, or unworkable ideas. Those all have some value since they at least form a basis for discussion as to how to solve a problem. The busy wait is the one idea that has harm above and beyond whether or not your program works. It's the one thing that I know of in code that can actually harm the system it is running on. That's why I chimed in. If you were to use goto, plenty of people would say that it was a bad thing to do, but it's a matter of opinion. There are reasons why goto has been replaced, but it does no fundamental harm. A busy wait is a different matter. Since you mentioned it, the idea was in your head. It was clear, by the time I saw the thread, that you were going to follow a better path for this problem, but that idea was still there, and you should be aware that it is a different class of bad.
    My usual boring signature: Nothing

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

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by chacham View Post
    And that's why i asked.

    I would suggest though, instead of saying something is wrong, show a better way of doing it, ideally together with a simple example like jmcilhinney gave above. It's the super simple examples that i learn the most from, as it allows me to grasp the concept easily. In this case, i knew i could use an event--which is why i asked--i just hadn't realized how, because i was caught up thinking about a loop. jmcilhinney pointed out the mistake, gave another way to look at it, and provided a simple example that illustrated the point. Easy to grasp and understand, i hope to implement it this afternoon.

    There are two examples in my signature.

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

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    I think more to the point where or who even taught you to use doevents?

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Anybody of a certain age will have used DoEvents. It was pretty much the only way to do some kinds of things in VB6. I had a use for it in a .NET application, but it wasn't a GOOD use for it. There are better alternatives, these days, and there were then, but that was a particularly simple way to solve one particular problem in one narrow situation. It's not an endorsement, but it is an understanding.
    My usual boring signature: Nothing

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by Shaggy Hiker View Post
    Anybody of a certain age will have used DoEvents. It was pretty much the only way to do some kinds of things in VB6. I had a use for it in a .NET application, but it wasn't a GOOD use for it. There are better alternatives, these days, and there were then, but that was a particularly simple way to solve one particular problem in one narrow situation. It's not an endorsement, but it is an understanding.
    It's important to understand the rules well in order to know when it's OK to break them.

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

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by Shaggy Hiker View Post
    Anybody of a certain age will have used DoEvents. It was pretty much the only way to do some kinds of things in VB6. I had a use for it in a .NET application, but it wasn't a GOOD use for it. There are better alternatives, these days, and there were then, but that was a particularly simple way to solve one particular problem in one narrow situation. It's not an endorsement, but it is an understanding.

    I started vb6 in 1996 at 13, iv never once used it.

  21. #21
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by ident View Post
    I started vb6 in 1996 at 13, iv never once used it.
    I guess you must of had a pre-pre-release.

    I remember buying VB5 the second week after it was released in 1997. I was using VB3 at that time, hadn't updated to VB4 because heard it was slower, etc...
    But when VB5 came out, if you had VB4 you could get VB5 for $100 off. Both VB4 and VB5 were on the shelf at the computer store, and VB4 cost $100 so essentially if you didn't already have VB4 you could buy it, and then get $100 off of VB5 so you got VB4 for "Free" since you would have had to pay the full price for VB5 if you didn't have VB4.
    I didn't purchase them that first week though, but since VB4 was the last to support 16-bit, and it wouldn't cost any more than not getting it, I went back to the store but couldn't find VB4 anywhere. When I asked, the sales clerk said that Microsoft had them pull VB4 off the shelves, I guess because I wasn't the only one who didn't have VB4 and saw a deal.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  22. #22
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    People remember too much. I can say that I used VB5 in 1997, because I moved across the country that year.
    My usual boring signature: Nothing

  23. #23
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Yeah, that's how I remember the time of a lot of things as well, because of my moving.
    I don't think it bothered my daughter too much, but as a result of my moving, she also has a few of those benchmarks as well, e.g.
    Code:
    Her school year:   Location:         Where I bought VB
    1,2                Maryland          (VB1 and 2)
    3,4                New York          (VB3 )
    5,6                Texas             (VB5)
    7,8                Pennsylvania 
    9....              New York          (VB6)
    Didn't realize until writing the list above, my purchases of version of VB tended to follow my daughters school grade.
    I held out for a while before I bought VB6, so I didn't get it in 1998. Probably more like 2001.
    Been "settled down" since 2001, no more big moves and been in the same house for 19 years. Was working for the same company for all those moves above, but did switch jobs in 2006, so have only worked for two entities (not counting the Navy years that preceded the above).
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

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

    Re: How to pause a loop until a variable gets set, or an event gets raised?

    Quote Originally Posted by passel View Post
    I guess you must of had a pre-pre-release.

    96/98, i dont write down memories from 22 years ago, got it from pc world for x-mas

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