Results 1 to 6 of 6

Thread: [RESOLVED] VBA DoEvents equivelent in VB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    210

    Resolved [RESOLVED] VBA DoEvents equivelent in VB

    I need a VBA DoEvents equivelent function from VB. Is there one? I have do while loops I need to allow processing of the screen.

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

    Re: VBA DoEvents equivelent in VB

    DoEvents exists in VB.NET, so you could just use that...but you shouldn't.

    DoEvents was largely a hack because VBA/VB6 is intended to be single threaded. People have figured out how to do multithreaded programming in VB6, though it's a bit awkward. VB.NET has several multithreaded options, which means that DoEvents is never necessary and is usually a bad idea. The problem with DoEvents is that it stops current processing while all pending messages get processed. What are those messages? You can't tell. What impact will they have? Once again you can't be certain. This can cause trouble as various UI events interact with one another.

    Perhaps the simplest alternative in a form is the BackgroundWorker. You could run a long-running loop in a background thread so that it doesn't impact the UI functionality. The problem with that would be that you can't interact with any UI elements from a different thread. To solve that problem, you can raise the ReportProgress event from the BGW, and that event will be raised on the UI thread. It can be a bit difficult to get your head around that, at first, as people tend to get confused as to what to do in the background thread and what to do in the UI thread, but once you get the hang of it, it is pretty easy to implement and is more efficient than DoEvents.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    210

    Re: VBA DoEvents equivelent in VB

    Quote Originally Posted by Shaggy Hiker View Post
    DoEvents exists in VB.NET, so you could just use that...but you shouldn't.

    DoEvents was largely a hack because VBA/VB6 is intended to be single threaded. People have figured out how to do multithreaded programming in VB6, though it's a bit awkward. VB.NET has several multithreaded options, which means that DoEvents is never necessary and is usually a bad idea. The problem with DoEvents is that it stops current processing while all pending messages get processed. What are those messages? You can't tell. What impact will they have? Once again you can't be certain. This can cause trouble as various UI events interact with one another.

    Perhaps the simplest alternative in a form is the BackgroundWorker. You could run a long-running loop in a background thread so that it doesn't impact the UI functionality. The problem with that would be that you can't interact with any UI elements from a different thread. To solve that problem, you can raise the ReportProgress event from the BGW, and that event will be raised on the UI thread. It can be a bit difficult to get your head around that, at first, as people tend to get confused as to what to do in the background thread and what to do in the UI thread, but once you get the hang of it, it is pretty easy to implement and is more efficient than DoEvents.
    Thanks Shaggy, good information.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: VBA DoEvents equivelent in VB

    Quote Originally Posted by Shaggy Hiker View Post
    To solve that problem, you can raise the ReportProgress event from the BGW, and that event will be raised on the UI thread.
    The DoWork event is raised on a background thread. You call the ReportProgress method in that event handler (or some method called from it) and that raises the ProgressChanged event on the UI thread.

    Follow the CodeBank link in my signature below and you'll find a thread there about using a BackgroundWorker. I know that there's at least one other thread in the CodeBank on the same subject.
    Last edited by jmcilhinney; Mar 15th, 2025 at 10:29 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: [RESOLVED] VBA DoEvents equivelent in VB

    That's a good point. JMC wrote a really nice tutorial on the BackgroundWorker. Whenever I forget something about that, I go to his link.
    My usual boring signature: Nothing

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

    Re: [RESOLVED] VBA DoEvents equivelent in VB

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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