Results 1 to 14 of 14

Thread: [RESOLVED] Continue Loop without event interruption

  1. #1

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Resolved [RESOLVED] Continue Loop without event interruption

    I apologize in advance if I'm being too stupid.

    Code:
     
       Public Event RunningPageConter(ByVal iPage As Integer)
    
       Do
            newStartPos = SendMessageLong(m_Settings.EditHwnd, EM_FORMATRANGE, False, _
                    VarPtr(m_Settings.FormatRange))
            If newStartPos >= TextLength Then
                Exit Do
            End If
            m_Settings.FormatRange.mCharRange.firstChar = newStartPos
            iPageCtn = iPageCtn + 1
            RaiseEvent RunningPageConter(iPageCtn)
       Loop
    I know that when the event fires, there is a pause in the loop... but what I wanted was for the loop not to stop, and for the event to continue without interrupting the loop.

    Is there any way to do this in VB6?

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Continue Loop without event interruption

    You need to explain what you are trying to accomplish. Right now, your loop makes recursive calls to the event.

    Without context it seems like you need to either:

    - Use just the loop and remove the RaiseEvent line from the loop
    or
    - Remove the loop and keep the RaiseEvent line

    Using both the loop and recursion can't be the right approach to whatever your end goal is.

  3. #3

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Continue Loop without event interruption

    Quote Originally Posted by OptionBase1 View Post
    You need to explain what you are trying to accomplish. Right now, your loop makes recursive calls to the event.
    The fired event will make a component draw the page view



    Quote Originally Posted by OptionBase1 View Post
    - Use just the loop and remove the RaiseEvent line from the loop
    or
    - Remove the loop and keep the RaiseEvent line
    I actually have two paths, I'm trying the second one now... but it's not like the alternatives you mentioned.... I can't remove the Loop... nor the event.


    Maybe if the event triggered a thread... then maybe it would be possible... but I don't understand much about it...

  4. #4
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Continue Loop without event interruption

    yes and no.
    u need to use multi-threading.
    should be available if u search.
    The Trick made something.

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: Continue Loop without event interruption

    Sounds like maybe you want PostMessage rather than SendMessage. They're declared the same except one is "Send" and the other is "Post". You've got your declaration as "SendMessageLong", so, if you change it, be sure you change the alias name as well as the DLL name in that declaration line.

    What's the difference? SendMessage waits until whatever is receiving the message has processed it. So, your code can get hung up, waiting on some other app to process your message. PostMessage just puts it into the Windows message queue and then immediately returns control to you. The main danger is, you might flood that other app with messages to the point that it's no longer processing them.

    Like others have said though, I don't really know what you're doing.

    EDIT: Also, unless your same VB6 program (i.e., thread) is receiving that message, you already have at least two threads running: one for your program and another for whatever program/driver/whatever is receiving that message.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: Continue Loop without event interruption

    Also, from another perspective, maybe it's that RaiseEvent that's slowing things down.

    A RaiseEvent is really no different than calling a Function (or Sub or Property), except that it's more like a child calling the parent rather than the parent calling the child.

    But, with respect to where the thread's program pointer is, whatever event you raised must run to completion, and then the code returns to the line after the RaiseEvent statement. That's just the fundamental idea of single-threaded. If you want to call RaiseEvent and have your loop continue before that event completes, then yeah, you're into multi-threading. The easiest way to do that is to just write a wholly separate program that you call in place of your RaiseEvent. (Or, maybe peel out this loop into a separate program, or out-of-process ActiveX EXE, which are separate threads).
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Continue Loop without event interruption

    What am I doing? A component for text editors.

    When the event fires, the event authorizes drawing the page view.
    Last edited by Episcopal; Sep 24th, 2022 at 02:04 PM.

  8. #8

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Continue Loop without event interruption

    Last edited by Episcopal; Sep 24th, 2022 at 02:06 PM.

  9. #9

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Continue Loop without event interruption

    Quote Originally Posted by Elroy View Post
    The easiest way to do that is to just write a wholly separate program that you call in place of your RaiseEvent.

    Elroy, you mean create two separate components, in this case 2 OCX??? That's it?

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: Continue Loop without event interruption

    Quote Originally Posted by Episcopal View Post
    Elroy, you mean create two separate components, in this case 2 OCX??? That's it?
    Nooo, an OCX (or, for that matter, DLLs) run in the same process (and, usually, the same thread) as your main program. To be in a different thread (for VB6), it's going to have to be an entirely different program. Or, using tools readily available with VB6, you could write an out-of-process ActiveX EXE (not DLL, not OCX) and it would be a different thread. The only other option is to use some very fancy stuff that The Trick has done to spawn additional threads within a VB6 process. The Trick's stuff works, but it's very advanced and I wouldn't recommend it.

    And just to be clear, running in the same thread = your code is only running in one (and only one) place at any point in time.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: Continue Loop without event interruption

    SOLUTION:

    DoEvent associated with common sense solves the problem, or almost. Microsoft Word also has the same shortcoming, as does my component. I will try to be an artist.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,855

    Re: [RESOLVED] Continue Loop without event interruption

    If you're going to use DoEvents, here's how I think of that call (the DoEvents call).

    I also think of it very similar to calling a function. It's just a function that processes all the events sitting in the Windows message queue (waiting to raise events) that apply to our thread, and then returns to the line following the DoEvents after all the events are processed. Again, that idea of single-threaded still applies.

    The biggest problem with DoEvents, is that you can quite easily (and unknowingly) get recursion going. For instance, let's say you have a button. When clicked, it executes some long-running process with a DoEvents in it. However, the user has clicked on your button 5 times. The first time through, it's going to hit that DoEvents, and then process the second button click, pausing the first execution of your long-running process. What's going to happen then is that the long-running process is going to start for a second time, even before the first time has finished.

    Then, if the user has truly clicked your button 5 times, that recursion will happen three more times. You can wind up with a true execution mess when using DoEvents, unless you are very careful.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  13. #13
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    Re: [RESOLVED] Continue Loop without event interruption

    Using Elroy's example, when I have a button that, when clicked, starts some long-running process, the first thing I do is disable that button and often most of the rest of the UI controls so that they can't be used again until the process finishes. The only thing that might get enabled is a Cancel or Stop button to abort the process.

  14. #14

    Thread Starter
    Fanatic Member Episcopal's Avatar
    Join Date
    Mar 2019
    Location
    Brazil
    Posts
    547

    Re: [RESOLVED] Continue Loop without event interruption

    Hello Elroy and jdc2000

    Yes, I know you have to be careful.

    When the text is modified, a function called ResetPageCounter is called, it returns True or False (Meanwhile the user cannot type anything). The algorithm mentioned above is capable of processing 300 pages in tenths of a second... the problem is when the number of pages exceeds 5000 pages...

    The DoEvent is only for rendering the page view, Office Word seems to do the same .... I did some tests here ...

    Thanks.

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