-
Mar 14th, 2025, 06:40 PM
#1
Thread Starter
Addicted Member
[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.
-
Mar 14th, 2025, 08:26 PM
#2
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
 
-
Mar 14th, 2025, 09:24 PM
#3
Thread Starter
Addicted Member
Re: VBA DoEvents equivelent in VB
 Originally Posted by Shaggy Hiker
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.
-
Mar 14th, 2025, 10:43 PM
#4
Re: VBA DoEvents equivelent in VB
 Originally Posted by Shaggy Hiker
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.
-
Mar 15th, 2025, 02:36 PM
#5
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
 
-
Mar 15th, 2025, 10:30 PM
#6
Re: [RESOLVED] VBA DoEvents equivelent in VB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|