Results 1 to 9 of 9

Thread: I need an explanation on this snippet of VB6 code

  1. #1

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    I need an explanation on this snippet of VB6 code

    Hi Experts,

    I have this small piece of code that I need explained.

    Code:
    Do While receiveMessages
        DoEvents
    Loop
    receiveMessages is a boolean variable.

    I have a user control that embeds into an existing app via implementation of interface.

    When my user control is embedded, the textbox in my user control could not respond to keyboard navigation and keyboard commands for copy and paste.
    In my investigation I found out that the usercontrol form receives the keyboard commands but not the textbox. That is, the textbox does not respond to WM_KEYUP events for arrow keys and ctrl or shift keys.
    Somehow the parent must be doing some stuff which I don't know.

    Having the above code somewhere in my user control fixes the textbox problem.

    My question is, what is DoEvents in a Loop doing?
    Does it somehow retain control to my user control and not the parent form where my user control is embedded?

    I really have no idea why this code is fixing the issue. Must be some underlying reason with the event handling and threading.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: I need an explanation on this snippet of VB6 code

    Before Win95, Windows could only do one thing at a time. This meant that events like mouse clicks and other user input could only be serviced in order, like everything else, even if they were in another program. A macro also has to be serviced in order, and while it was running, the code that processed events could not run. To solve this, the DoEvents command allowed Windows to take a break from the macro, process any pending events (even in other programs), and then return control to the macro. It is still somewhat useful today because while Windows can handle the events without worrying about a macro bogging it down, an individual program like Access is still limited.

    In the modern era, DoEvents is a relic of the old Windows that will slowly disappear as things like .NET supplant VBA and the old event processing infrastructure.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need an explanation on this snippet of VB6 code

    VB is not multithreaded. Events/procedure/stack processing is LIFO... so while your still in a procedure other events, e.g. keyboard events, messages are not processed and are queued. DoEvents within procedure allows windows messages in queue to be processed before resuming next statement in procedure.

  4. #4

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: I need an explanation on this snippet of VB6 code

    Hi koolskid, I could not rate you for now on your reply.

    But so you know I really appreciate it!

  5. #5

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: I need an explanation on this snippet of VB6 code

    So what are the possible scenarios that keyboard events for one control gets wiped out or not processed?

    Is it some other call that somehow polls these events and then removes it from the queue?

    Or is it a windows fail safe where a windows command can only stay within the queue for so long?

    Right now I am assuming that some other process somehow inspects all keyboard events and removes it from the queue. But I'm probably wrong.

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: I need an explanation on this snippet of VB6 code

    Windows messages are handled by the operating system, along with its routing, queung per window, cascade to elements within window, etc.

  7. #7
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: I need an explanation on this snippet of VB6 code

    DoEvents in action
    Without DoEvents only part of the the form is hidden.
    Code:
    Me.Visible = False
    'DoEvents
    Sleep 1800
    Me.Visible = True
    Work correctly when DoEvents is added
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: I need an explanation on this snippet of VB6 code

    In .NET its still in existance.

    Code:
    Application.DoEvents()
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: I need an explanation on this snippet of VB6 code

    Quote Originally Posted by oceanebelle
    Hi koolskid, I could not rate you for now on your reply.

    But so you know I really appreciate it!
    Thats ok... How about an e-burger

    Damn I am very hungry right now....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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