Page 1 of 2 12 LastLast
Results 1 to 40 of 58

Thread: VB6 Threaded Forms (simple Demo)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    VB6 Threaded Forms (simple Demo)

    As the title says, just a simple Demo which might help to "find ground again", when it comes to VB6-threading.
    (there's certainly a whole lot of overcomplicating, unnecessary stuff out there).

    The Demo shows real InProcess-Multithreading via a simple VB6-AX-Exe-Project.

    There are no API-calls necessary, nor are there any Assembly-hacks - it's just plain VB6 - running threaded Forms in STAs.

    Within the threaded Form, an external (OCX-based) Control gets loaded (since that recently came up).
    To get things running:
    - open and compile ExternalControl.vbp first
    - then open and compile AxThreading.vbp

    Ok, here the Zip:
    AxExeThreading.zip

    Addition:
    Since the question came up, how to work with "Project-Private UserControls" (instead of loading
    a Control
    from an external OCX onto the threaded Form, as covered in the original Demo-Zip above)...

    Here a slightly changed version, which covers that case (by hosting the Thread-Forms, as well as the Private UCs in a separate AX-Dll-Project):

    AxExeThreading2.zip


    Have fun,

    Olaf
    Last edited by Schmidt; Feb 5th, 2020 at 12:58 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,375

    Re: VB6 Threaded Forms (simple Demo)

    I think the overcomplicating comes, because of the following disadvantages that comes from using the simple plain VB6 approach:
    - AxExe can't be RegFree
    - The OCX that is used within the AxExe must be pre-compiled. Thus not very flexible when changing logic quite often in the threaded process.

    That's why people like generic solutions. However, this is just a note of the disadvantages with this approach for general info and not meant to hijack the thread.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by Krool View Post
    I think the overcomplicating comes, because of the following disadvantages that comes from using the simple plain VB6 approach:
    - AxExe can't be RegFree
    That's true - but on the other hand, only a single start with "run as Admin" would be required, to make things work.

    And as said in the other thread...
    This AxExe-approach is a good (API-free) way to learn how to handle the STA-based VB6-threading.
    It is also quite easy, to "move stuff from the AxExe" over into an AX-Dll (without larger code-changes),
    and then do the threading via a normal StdExe + Ax-Dll-based STA-thread(instances).

    Quote Originally Posted by Krool View Post
    - The OCX that is used within the AxExe must be pre-compiled. Thus not very flexible when changing logic quite often in the threaded process.
    This can be easily covered, by developing/debugging these things in a VB6-ProjectGroup.
    In the concrete example at hand, you will only have to add the existing "ExternalControl.vbp" to the opened AxThreading.vbp
    (and save this Group as e.g. "_Debug.vbg").

    Olaf

  4. #4

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    What can i know if i work with an object in other STA? VB6 hides all the things
    ...seems we can't learn how to handle the STA-based VB6-threading because it isn't differ from usual object.
    But exactly that (the fact that the ThreadObject is "just another Object") is the beauty of the VB6-STA-based threading-approach.
    Why give it away?

    These STA-threads are basically like "a process" (with regards to "isolated memory-management" via TLS).

    And no, there's still something to learn, when working with these "STA-Objects".

    These Objects already cover "synchronous communication" out of the box automatically (after you instantiated them on their own STA) -
    so there's no need, to write any "special sync-code" via Mutexes or CriticalSections (as needed in FreeThreading-approaches).

    What these Thread-Objects don't cover though (the part which needs to be "learned"), is -
    how to trigger actions on them in an asynchronous fashion (whilst providing the Thread with the PayLoad-data it has to process).

    Once that "async-part" was learned (not contained in the demo-example here), the shown approach
    (no matter if handled within an Ax-Exe or via Ax-Dlls) is a very robust way to cover nearly all threading-scenarios in VB6.

    Olaf
    Last edited by Schmidt; Jan 24th, 2020 at 12:03 PM.

  6. #6
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    But exactly that (the fact that the ThreadObject is "just another Object") is the beauty of the VB6-STA-based threading-approach.
    Why give it away?
    The threading in COM is very big area. You told about learning threading-handle. What's the threading handle differs from usual object? I understand the VB6 hides many things like message-pumping, marhsling, etc. Maybe it's useful for "just use" but i think this isn't useful for learning.

    These STA-threads are basically like "a process" (with regards to "isolated memory-management" via TLS).
    This is many pitfails like if you use thread-pool then you can got the same STA and your global variables become shared with others.

    These Objects already cover "synchronous communication" out of the box automatically (after you instantiated them on their own STA) -
    so there's no need, to write any "special sync-code" via Mutexes or CriticalSections (as needed in FreeThreading-approaches).
    This is the basic COM rule. The COM ensures that - not VB6. If you use for example C++ you also don't need "special sync-code" if you use a marshaled COM object. The potential "learner" don't know about that things at all in VB6 - the what i tell about.

    What these Thread-Objects don't cover though (the part which needs to be "learned"), is -
    how to trigger actions on them in an asynchronous fashion (whilst providing the Thread with the PayLoad-data it has to process).
    Generally this task isn't related to multithreading and especially COM/STA/etc. You can use that scenario in a single threaded application. The typical task - accept windows messages which are placed asynchronously.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    The threading in COM is very big area.
    Yes, threading in COM is a wide field - but that's not what I thought we were talking about (in the context of this Demo-post here).
    I'm talking specifically about "VB6-STA-based threading, the easy way" (as demonstrated in the AxExe-based example in Post #1 here).

    Quote Originally Posted by The trick View Post
    You told about learning
    Maybe it's useful for "just use" but i think this isn't useful for learning.
    Exactly! ... "just use" these (Thread-)Objects like any other (normal) Object.
    A VB6 beginner (or hobbyist) does not need to know (in detail) what an STA is, or how TLS works (underneath).

    All he needs to know is, that CreateObject(AxExe.ProgID) will create an Object on its own Thread-Apartment,
    present in the calling main-thread via a returned Object-Ref, which represents already a "communication-interface to the real Object in said thread".

    Threading can really be that easy (when one uses VB6 AxExes).

    And a StdExe+AXDll approach can be made to behave in the very same way (when we talk about User-Code).
    One just needs to provide a Helper-routine, which will return a Proxy-instance in the same way as the CreateObject-call does in an AXExe
    (which would make a switch to a real regfree working version easier - because the UserCode from the AxExe can mostly remain the same).

    BTW - I don't know what got you so "riled up" here - but I assume it was my mentioning of "ASM-hacks".
    And yes, I don't like them - and don't use them personally for several (good) reasons.

    They are mostly used, to "avoid additional Dll-dependencies" (e.g. written in a more suitable language, without any need for ASM-usage there).
    And this stance of "avoiding Dll dependencies like the plague" is (IMO) one of the main-reasons,
    why the VB6-community remains stuck in that "Single-Exes are all I know and want"-universe.

    Olaf

  8. #8
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    Yes, threading in COM is a wide field - but that's not what I thought we were talking about (in the context of this Demo-post here).
    I'm talking specifically about "VB6-STA-based threading, the easy way" (as demonstrated in the AxExe-based example in Post #1 here).
    Notice what's i quoted in the first post. It was about learning how to handle the STA-based VB6-threading.

    Exactly! ... "just use" these (Thread-)Objects like any other (normal) Object.
    A VB6 beginner (or hobbyist) does not need to know (in detail) what an STA is, or how TLS works (underneath).
    Exactly what i told about in the first post. One doesn't not need to know about STA, etc. What he/she can learn regarding threading? The question i asked in the first post.

    And a StdExe+AXDll approach can be made to behave in the very same way (when we talk about User-Code).
    One just needs to provide a Helper-routine, which will return a Proxy-instance in the same way as the CreateObject-call does in an AXExe
    (which would make a switch to a real regfree working version easier - because the UserCode from the AxExe can mostly remain the same).
    Yes, you're right. I didn't argue with that.

    BTW - I don't know what got you so "riled up" here - but I assume it was my mentioning of "ASM-hacks".
    No. It was when you said the all other methods are "inherently instable and not generically usable":
    Any attempts at "FreeThreading" or "VB6-Runtime-hacks via ASM" are inherently instable and not generically usable.
    Although you told about "by doing the usual thing, handling the incoming thread-call via a TypeLib-defined PostMessage-delegation". Isn't that the VB6-Runtime-Hack?

    They are mostly used, to "avoid additional Dll-dependencies" (e.g. written in a more suitable language, without any need for ASM-usage there).
    Does it bother you that VB6/MSVBVM60 itself uses this? I mean ASM-thunks etc.

    And this stance of "avoiding Dll dependencies like the plague" is (IMO) one of the main-reasons,
    why the VB6-community remains stuck in that "Single-Exes are all I know and want"-universe.
    It doesn't matter for our discussion. I didn't argue with that and moreover i have other opinion about that.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    Notice what's i quoted in the first post. It was about learning how to handle the STA-based VB6-threading.
    What he/she can learn regarding threading? The question i asked in the first post.
    Ok, then again (slowly - your english-comprehension is certainly not the best)...
    What I wrote - and what you quoted was:
    "This AxExe-approach is a good (API-free) way to learn how to handle the STA-based VB6-threading."

    So I was apparently talking about the AX-Exe-based threading-approach (in the context of the posted Demo-Code).
    (which hides "all the low level STA- and TLS-stuff" from the User of this approach - but "STA-stuff it is, all the same").

    So, the only thing a User of that approach has to learn is:
    - how to deal with the "Object-representation" (of the Main-Object which was created on such an STA)
    (which is "simple enough" with regards to the learning-efforts - but "time to learn it" has to be invested all the same)

    What a potential User of this AxExe-approach has *not* to learn is:
    - how STAs work in detail
    - how TLS works in detail
    - how to hack the vb6Runtime
    - how to maintain ASM-code-snippets (when certain things change, or are buggy)
    - or how to avoid "certain things" which might be instable with runtime-hacked threading

    I mean, all those "jsvenu-threads" in the main-forum were going on, for nearly a hundred postings -
    and still he does not have managed his threading-scenario (using an "advanced approach").

    And I'm pretty sure, he's not the only one - my guess is, that 95% of the Users "just want to use threads".
    And I'm also quite sure, that most of these Users would be able, to get an adaption of the simple approch (as shown here) -
    to work for them in their own threading-scenarios (without crashes or instabilities).

    So, "learning they have to do" - but far less than with the "other alternatives, which overcomplicate things" (IMO).
    I hope you understood it now more clearly (having these 95% of "normal VB6-Users in mind").

    Quote Originally Posted by The trick View Post
    It was when you said the all other methods are "inherently instable and not generically usable":
    Ok - so you're sure, that your modMultiThreading2.bas-module is now entirely bug-free?
    No matter "which Project-Type it is used from" (Ax-Exe, Ax-Dll, Std-Exe)?


    Quote Originally Posted by The trick View Post
    ...you told about "by doing the usual thing, handling the incoming thread-call via a TypeLib-defined PostMessage-delegation".
    Isn't that the VB6-Runtime-Hack?

    Does it bother you that VB6/MSVBVM60 itself uses this? I mean ASM-thunks etc.
    It is not my intention, to "rile you up" - I'm arguing from a point of "reliable usage" (for those afore-mentioned "95% of VB6-Users").

    And no, TypeLib-calls are definitely not "hacks" -
    they work entirely within the "normal VB6- and COM-realm and ABI" (no matter if those calls are defined with "avoid Err-checking", or not).

    It is "usage of predefined functionality".
    Hacks are, when you "change predefined functionality" (which you do..., e.g. when you manipulate around at the TLS-slots and whatnot in the vbRuntime itself).

    Note that msvbvm60.dll is still maintained (it is part of system-updates) - and so it might change its internal behaviours, structures and offsets...

    So, no - I'm still not a fan of such hacks - especially in cases, where they don't offer much over other, simpler alternatives which "play by the rules".

    Olaf

  10. #10
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    new method download (can you test?):
    createthread for load new form with ocx or usercontrol demo:

    use hook api for fix eip
    (EbLoadRunTime
    EbCreateContext
    EbSetContextWorkerThread)
    i don't khnow your project can support usercontrol?

    http://www.vbforums.com/showthread.p...ol)&highlight=

    http://www.vbforums.com/attachment.p...3&d=1579799200
    Last edited by xiaoyao; Jan 25th, 2020 at 07:53 PM.

  11. #11
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    how to add usercontrols on forms object in thread?
    (your project can add ocx,but can't support vb6 usercontrol)
    Code:
    Code:
    Private obj As Object
    
    Private Sub Command1_Click()
    On Error GoTo ERR
    
    Set obj = Me.Controls.Add("Project1.UserControl1", "UserControl1B")
    obj.Visible = True
    MsgBox obj.Width
      Exit Sub
    ERR:
      MsgBox "ERR:" & ERR.Number & "," & ERR.Description
    End Sub
    
    sub test2
    Dim ctlName As Control
    Set ctlName = Form1.Controls.Add("Project1.UserControl1", "Text1", Form1)
    ctlName.Visible = True
    end sub
    download【http://www.vbforums.com/attachment.p...059696】
    Last edited by xiaoyao; Jan 26th, 2020 at 08:47 PM. Reason: wrong word,fix ok

  12. #12
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    So, the only thing a User of that approach has to learn is:
    - how to deal with the "Object-representation" (of the Main-Object which was created on such an STA)
    (which is "simple enough" with regards to the learning-efforts - but "time to learn it" has to be invested all the same)
    Please read my post carefully. I won't touch your "english-comprehension" but i didn't get the answer to my question yet. What's the difference if an user works with usual object between your "ThreadObject"? You wrote - there is no difference and it's good. I know about this but i answered then what the user learn about? There is NO difference if the user works for example with Word.Application. Do you think the user thought whenever he work with threading in this case?

    What a potential User of this AxExe-approach has *not* to learn is:
    - how STAs work in detail
    - how TLS works in detail
    - how to hack the vb6Runtime
    - how to maintain ASM-code-snippets (when certain things change, or are buggy)
    - or how to avoid "certain things" which might be instable with runtime-hacked threading
    An user shouldn't know how the runtime works and your last 4 points isn't related to VB6-STA-COM-threading.
    From your example an user won't know:
    - What's the Apartment model;
    - What models exist;
    - What's the difference between VB6-threading models;
    - What's the marshaling, why we need to use that;
    - What's the message pumping. Why when i use a long-running cycle/sleeping my threads are freezes?
    - Why my global variables are always "zeroed"? Why sometimes they are "not-zeroed"?
    - Why can't i accept some callbacks and my application crashes?
    - . . .
    - . . .

    I mean, all those "jsvenu-threads" in the main-forum were going on, for nearly a hundred postings -
    and still he does not have managed his threading-scenario (using an "advanced approach").
    Please study his thread if you want to talk about "jsvenu-threads". He want to use "free-threading" which isn't acceptable in VB6 (only in the restricted scenarios).

    And I'm pretty sure, he's not the only one - my guess is, that 95% of the Users "just want to use threads".
    And I'm also quite sure, that most of these Users would be able, to get an adaption of the simple approch (as shown here) -
    to work for them in their own threading-scenarios (without crashes or instabilities).
    I already wrote your example is perfect to "just use". You can write me statements twice but i understand the first time.

    So, "learning they have to do" - but far less than with the "other alternatives, which overcomplicate things" (IMO).
    I hope you understood it now more clearly (having these 95% of "normal VB6-Users in mind").
    I already wrote they learn nothing regarding to threading from your example.

    Ok - so you're sure, that your modMultiThreading2.bas-module is now entirely bug-free?
    No matter "which Project-Type it is used from" (Ax-Exe, Ax-Dll, Std-Exe)?
    No. I'm not sure and i already wrote you. I won't duplicate the posts. Do you think your solution is bug-free?

    It is not my intention, to "rile you up" - I'm arguing from a point of "reliable usage" (for those afore-mentioned "95% of VB6-Users").
    Okay but please see to thread you wrote. The thread about creation Native DLL and avoiding TypeLibs/etc.

    And no, TypeLib-calls are definitely not "hacks" -
    they work entirely within the "normal VB6- and COM-realm and ABI" (no matter if those calls are defined with "avoid Err-checking", or not).
    Hmmm. I don't tell about TypeLib usage in common sense rather threading one. Why then you use it at free-threading manner? Isn't a hack?

    It is "usage of predefined functionality".
    Hacks are, when you "change predefined functionality" (which you do..., e.g. when you manipulate around at the TLS-slots and whatnot in the vbRuntime itself).
    Who manipulate TLS?

    Note that msvbvm60.dll is still maintained (it is part of system-updates) - and so it might change its internal behaviours, structures and offsets...
    Yes. Even moreover they do it but they never broke the backward-compatibility. But what's changed if for example the some structure is changed?

    So, no - I'm still not a fan of such hacks - especially in cases, where they don't offer much over other, simpler alternatives which "play by the rules".
    The solution is selected from task and preferences. Your solution isn't cover all the tasks and preferences and isn't alternative. I understand it and use the solution which is suitable for the current task. You can use what you want.

  13. #13
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    I think there will always be a way ,no need tls?
    a simple method for support show forms with vb6 usercontrol or ocx in new thread.

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    Please read my post carefully. I won't touch your "english-comprehension" ...
    But it *is* your bad english-comprehension which caused all this "off-topic talking in circles".

    You seemed to think, I wanted to explain and teach about "STAs".

    But that's not what I said... here again what I wrote:
    "This AxExe-approach is a good (API-free) way to learn how to handle the STA-based VB6-threading."

    Note the parts I've marked blue, because they take "precedence" (grammar-wise).

    It's basically the same as the following sentence:
    "This is a good way to learn how to handle combustion-engine-based cars."

    It's all about "car-handling" - not about "the inner workings of a combustion-engine".

    HTH

    Olaf
    Last edited by Schmidt; Feb 5th, 2020 at 01:52 PM.

  15. #15
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by Schmidt View Post
    As the title says, just a simple Demo which might help to "find ground again", when it comes to VB6-threading.
    (there's certainly a whole lot of overcomplicating, unnecessary stuff out there).

    The Demo shows real InProcess-Multithreading via a simple VB6-AX-Exe-Project.

    There are no API-calls necessary, nor are there any Assembly-hacks - it's just plain VB6 - running threaded Forms in STAs.

    Within the threaded Form, an external (OCX-based) Control gets loaded (since that recently came up).
    To get things running:
    - open and compile ExternalControl.vbp first
    - then open and compile AxThreading.vbp

    Ok, here the Zip:
    AxExeThreading.zip

    Addition:
    Since the question came up, how to work with "Project-Private UserControls" (instead of loading
    a Control
    from an external OCX onto the threaded Form, as covered in the original Demo-Zip above)...

    Here a slightly changed version, which covers that case (by hosting the Thread-Forms, as well as the Private UCs in a separate AX-Dll-Project):

    AxExeThreading2.zip


    Have fun,

    Olaf
    Dear olaf,

    I changed the AxExeThreading2 activex exe project in AxExeThreading2.zip to standard exe .
    When I run this standard exe application I am able to display two forms in different threads which can be observed on the caption of the form which displays thread ID and the two forms are showing different thread IDs in their caption.But when we try to switch between forms by clicking on them we get the following error (in msgbox):

    the action cannot be completed because other application is busy ...

    What are the changes to be made in code for this standard exe to create new form with user control in new thread without using vbheader in a simple way without displaying the following error(in msgbox) when we click on different forms displayed in different threads for switching between them.



    regards,
    JSVenu
    Last edited by jsvenu; Feb 8th, 2020 at 07:23 AM.

  16. #16
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by Schmidt View Post
    As the title says, just a simple Demo which might help to "find ground again", when it comes to VB6-threading.
    (there's certainly a whole lot of overcomplicating, unnecessary stuff out there).

    The Demo shows real InProcess-Multithreading via a simple VB6-AX-Exe-Project.

    There are no API-calls necessary, nor are there any Assembly-hacks - it's just plain VB6 - running threaded Forms in STAs.

    Within the threaded Form, an external (OCX-based) Control gets loaded (since that recently came up).
    To get things running:
    - open and compile ExternalControl.vbp first
    - then open and compile AxThreading.vbp

    Ok, here the Zip:
    AxExeThreading.zip

    Addition:
    Since the question came up, how to work with "Project-Private UserControls" (instead of loading
    a Control
    from an external OCX onto the threaded Form, as covered in the original Demo-Zip above)...

    Here a slightly changed version, which covers that case (by hosting the Thread-Forms, as well as the Private UCs in a separate AX-Dll-Project):

    AxExeThreading2.zip


    Have fun,

    Olaf
    Dear Olaf,

    Finally I solved the problem of using usercontrol in the same activex exe as follows without having to create new activex dll as follows using Trick's help(code attached):

    Add the following method to cThread

    Code:
    Public Sub SetThisThreadToApartmentModel()
        hModule = App.hInstance
        modMultiThreading.GetVBHeader
    End Sub
    Call the above method in cmdCreateThreadedForm_Click as follows:

    Code:
    Private Sub cmdCreateThreadedForm_Click()
        Dim Thread As cThread
        Set Thread = CreateObject(GetProjectLibName & ".cThread")                   'create a new Thread on its own STA
        Thread.SetThisThreadToApartmentModel                                        'set this thread to Apartment model
        Thread.CreateAndShowThreadedForm                                            'create a new Form within that STA by calling a Public Method of the ThreadInstance
        ThreadInstances.Add Thread                                                  'store this ThreadInstance within a Thread-Collection (for later re-usage)
    End Sub
    Here

    Code:
    Public Function GetVBHeader() As Long
    
        
        Dim ptr     As Long
        Dim bThreading As Long
        Dim tempheader As Long
        Dim lOldProtect     As Long
        ' // Get e_lfanew
        GetMem4 ByVal hModule + &H3C, ptr
        ' // Get AddressOfEntryPoint
        GetMem4 ByVal ptr + &H28 + hModule, ptr
        ' // Get VBHeader
        GetMem4 ByVal ptr + hModule + 1, tempheader
        GetMem1 ByVal tempheader + &H3C, bThreading
        bThreading = bThreading Or 1
        VirtualProtect ByVal tempheader, 4, PAGE_READWRITE, lOldProtect
        GetMem1 bThreading, ByVal tempheader + &H3C                     'set to apartment model as per Trick's help
        VirtualProtect ByVal ptr, 4, lOldProtect, 0
        GetVBHeader = tempheader
    End Function
    Thankyou Olaf one again for the example.


    Dear Trick,


    You have provided multithreading examples in which each thread has a copy of the main vbheader modified using CreateVBHeaderCopy module function which takes care of avoiding global variables from being cleared.

    Can you clarify me since we are using vbheader only in the new thread without using CreateVBHeaderCopy module function how we can avoid clearing of global variables here in this attached activex exe application.





    regards.

    JSVenu
    Attached Files Attached Files
    Last edited by jsvenu; Feb 10th, 2020 at 01:43 AM.

  17. #17
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    But that's not what I said... here again what I wrote:
    "This AxExe-approach is a good (API-free) way to learn how to handle the STA-based VB6-threading."
    Olaf,
    I understood what you said. I just asked you - "what's exactly"? The working with "your" threading isn't differs from an usual object or any other AxExe. It's the same if i'd make like CreateObject("Word.Application"). Do you think someone think about threading when works with Word.Application? It's the same. There is NO difference between usual object and your threading object. And i asked what's the exactly a potential user can learn if it's the same. As i wrote before - "just for use". Threading for threading.

    "This is a good way to learn how to handle combustion-engine-based cars."

    It's all about "car-handling" - not about "the inner workings of a combustion-engine".
    But your example looks like learning toy-car-handling. We don't have the steering wheel, the brakes, etc. We just "handle" the cars by pushing - they are going.

  18. #18
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    CreateThread by Activex Exe In vb6(Support Shared variables)-VBForums
    http://www.vbforums.com/showthread.p...69#post5451469

    Code:
    ActivexExeThread.bas
    Public Src As String '主变量
    Public MEM(0) As String
    Public x As Long
    
    Public ABC As Long, X2 As Long
    Public AbcArr(0) As Long
    
    in thread forms:
    Private Sub ShowSrcAbc_Click()
    Dim OldAbc As Long
    'AbcArr(X2)=abc
    'MEM(x) =src
    OldAbc = AbcArr(X2)
    MEM(x) = "test:" & Now
    AbcArr(X2) = AbcArr(X2) + 1
    MsgBox "Src=" & MEM(x) & vbCrLf & "Old Abc=" & OldAbc & vbCrLf & "New ABC=" & AbcArr(X2), , "ThreadID = " & App.ThreadID
    End Sub
    
    in main form:
    MsgBox "Src=" & Src & vbCrLf & "ABC=" & ABC, , "threadid=" & App.ThreadID

  19. #19
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by xiaoyao View Post
    CreateThread by Activex Exe In vb6(Support Shared variables)-VBForums
    http://www.vbforums.com/showthread.p...69#post5451469

    Code:
    ActivexExeThread.bas
    Public Src As String '主变量
    Public MEM(0) As String
    Public x As Long
    
    Public ABC As Long, X2 As Long
    Public AbcArr(0) As Long
    
    in thread forms:
    Private Sub ShowSrcAbc_Click()
    Dim OldAbc As Long
    'AbcArr(X2)=abc
    'MEM(x) =src
    OldAbc = AbcArr(X2)
    MEM(x) = "test:" & Now
    AbcArr(X2) = AbcArr(X2) + 1
    MsgBox "Src=" & MEM(x) & vbCrLf & "Old Abc=" & OldAbc & vbCrLf & "New ABC=" & AbcArr(X2), , "ThreadID = " & App.ThreadID
    End Sub
    
    in main form:
    MsgBox "Src=" & Src & vbCrLf & "ABC=" & ABC, , "threadid=" & App.ThreadID
    http://www.vbforums.com/showthread.p...=1#post5451679 #2

  20. #20
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Dear Trick,

    In the activex exe application supporting usercontrol once we display user control in additional thread forms when we close the application it is still running.Can you provide code for properly closing the application so that it closes properly.

    Link: http://www.vbforums.com/attachment.p...1&d=1581403109

    regards,
    JSVenu
    Attached Files Attached Files
    Last edited by jsvenu; Feb 11th, 2020 at 01:40 AM.

  21. #21
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by jsvenu View Post
    Dear Trick,

    In the activex exe application supporting usercontrol once we display user control in additional thread forms when we close the application it is still running.Can you provide code for properly closing the application so that it closes properly.

    Link: http://www.vbforums.com/attachment.p...1&d=1581403109

    regards,
    JSVenu
    createthread(activex.exe),why threadid is the same?
    Attached Images Attached Images  

  22. #22
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by jsvenu View Post
    Dear Trick,

    In the activex exe application supporting usercontrol once we display user control in additional thread forms when we close the application it is still running.Can you provide code for properly closing the application so that it closes properly.

    Link: http://www.vbforums.com/attachment.p...1&d=1581403109

    regards,
    JSVenu
    Dear Trick,

    When I tried to use the following code for properly unloading still the application is running when I close the application after displaying a usercontrol by setting the form thread to apartment model.I had to use task manager to kill the running activex exe application.Please clarify.

    Code:
    Public Declare Function VBDllCanUnloadNow Lib "MSVBVM60.DLL" (ByVal pVbHeader As Long) As Long
    
    Public Sub UnloadThreadedForm()
        If Not ThreadForm Is Nothing Then Unload ThreadForm: Set ThreadForm = Nothing
        ' // The runtime will call CThreadPool::CheckForProjectUnload and CVBThreadAction::CleanupProjData
        If VBDllCanUnloadNow(modMultiThreading.GetVBHeader) Then
            Exit Sub
        End If
    End Sub
    regards,
    JSVenu

  23. #23
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by xiaoyao View Post
    createthread(activex.exe),why threadid is the same?
    dear Xiaoyao,
    you might have changed project setting to thread pool.Change to thread per object and try.
    regards,
    JSVenu

  24. #24
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    can you testing?I try ,it's error happened same threadid

  25. #25
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    Olaf,
    I understood what you said. I just asked you - "what's exactly"? The working with "your" threading isn't differs from an usual object or any other AxExe. It's the same if i'd make like CreateObject("Word.Application"). Do you think someone think about threading when works with Word.Application? It's the same. There is NO difference between usual object and your threading object. And i asked what's the exactly a potential user can learn if it's the same. As i wrote before - "just for use". Threading for threading.


    But your example looks like learning toy-car-handling. We don't have the steering wheel, the brakes, etc. We just "handle" the cars by pushing - they are going.
    I don't known how to run sta threads in standard exe?For example, some multithreading needs to have access to global variables, and some are completely isolated from each other. (比如有的多线程需要可以访问全局变量,有的彼此完全隔离。)

  26. #26
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by xiaoyao View Post
    can you testing?I try ,it's error happened same threadid
    Dear xiaoyao,

    Have you tried the same attachment. http://www.vbforums.com/attachment.p...1&d=1581403109
    Just make exe and run the compiled exe.

    regards,
    JSVenu

  27. #27
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by jsvenu View Post
    Dear Trick,

    When I tried to use the following code for properly unloading still the application is running when I close the application after displaying a usercontrol by setting the form thread to apartment model.I had to use task manager to kill the running activex exe application.Please clarify.

    Code:
    Public Declare Function VBDllCanUnloadNow Lib "MSVBVM60.DLL" (ByVal pVbHeader As Long) As Long
    
    Public Sub UnloadThreadedForm()
        If Not ThreadForm Is Nothing Then Unload ThreadForm: Set ThreadForm = Nothing
        ' // The runtime will call CThreadPool::CheckForProjectUnload and CVBThreadAction::CleanupProjData
        If VBDllCanUnloadNow(modMultiThreading.GetVBHeader) Then
            Exit Sub
        End If
    End Sub
    regards,
    JSVenu
    Dear Trick,

    Can we not close the application properly in which we have threads with vbheader set to apartment model like above by unloading all the resources like above and see that the application closes completely without running as we see it running in task manager.

    regards,
    JSVenu

  28. #28
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: VB6 Threaded Forms (simple Demo)

    I like postmessage wm_quit to main form,
    but your project not support,so can't end process

  29. #29
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by xiaoyao View Post
    I like postmessage wm_quit to main form,
    but your project not support,so can't end process
    Dear Xiaoyao,

    I think we can use ExitThread API with 0 as the parameter after calling VBDllCanUnloadNow in post #22 for closing the additional threads which are set to apartment model (to support user control) for which Trick may suggest any changes.

    regards,
    JSVenu

  30. #30
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Dear Trick,

    In the link below
    http://www.vbforums.com/attachment.p...3&d=1581316682

    When we run the activex exe application with thread per object setting and try to display a new form in new thread by clicking on Create and show an additional threaded Form button of main form we get a new form displayed in new thread and it supports loading user control at runtime which can be verified by clicking on LoadUsercontrol in the new displayed form.Upto here everything is fine.

    But from now onwards when we again try to display a new form in new thread by clicking on

    Create and show an additional threaded Form button of main form we get a new form displayed in same main thread but not new thread.Ofcourse it supports user control loading at runtime.

    Before displaying I was setting the thread to apartment model by modifying vbheader.

    Even though the above activex exe supports loading user control of the same project at runtime why I get new form in new thread only first time and after that the new form is always created in the main thread only.This can be checked by seeing new form's thread id in its Caption.

    Here I think as you associate new vbheader copy with each thread I have to use new vbheader copy for each thread as you already showed in standard exe.

    Can you show me how to code such that we have new vbheader copy for each thread in activex exe application so that the activex exe application creates new form in new thread whenever I click on "Create and show an additional threaded Form" button in main thread default form so that the new form displayed in new thread supports runtime user control loading for which I shall be thankful to you.

    regards,
    JSVenu
    Last edited by jsvenu; Feb 26th, 2020 at 12:25 PM.

  31. #31

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by jsvenu View Post
    Dear Trick,

    ...
    Can you show me how to code such that we have new vbheader copy for each thread in activex exe application...
    I can only repeat what I've posted already several times...

    Please don't use these "vbHeader-hacks", because:
    - they introduce instabilities
    - they crash outright (e.g. when you compile to PCode)
    - and are not needed at all, for proper threading in VB6

    The whole CodeBank-entry here was provided, to show a truly safe way with VB6-threading (avoiding the hacks).

    I've even addressed your "Thread-Form-issues" (with Project-Private UserControls) appropriately,
    by uploading a second example with support for that scenario (by encapsulating these thread-forms in their own Ax-Dll).

    ActiveX-Dlls are (generally) the proper place for "VB6-WorkerThread-code" (including Form-Definitions, when they shall run on their own thread)-
    And such AX-Dll-hosted WorkerThread-code will run properly, even when you use VB6-STA-based threading without AxExes.

    Ax-Dlls can easily be shipped in a \Bin\-folder beside your "normal StdExe-binary" in a true portable fashion (no registering needed).
    I have no clue, why so many in the VB-community are trying to avoid writing their own Ax-Dlls - there's absolutely no reason for that.

    HTH

    Olaf

  32. #32

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    ...your example looks like learning toy-car-handling.
    Nope, cannot agree with that.

    The built-in threading-support (via COM-STAs and their automatic synchronizing)
    is (for me) comparable with a "Luxus-Van" ...

    It has a whole lot of comfort- and safety-features built-in (comparable to "ESP and ABS" + "airbags in every corner") -
    and it will cover 99% of all threading-scenarios (on normal roads).

    Whereas "the hacks" look like some "go-cart, kept together with a lot of duct-tape, always hoping the wheels don't fly off".

    If you really have to go "offroad" with your threading, then there's better suited (language-)vehicles out there.

    Olaf

  33. #33
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by Schmidt View Post
    I can only repeat what I've posted already several times...

    Please don't use these "vbHeader-hacks", because:
    - they introduce instabilities
    - they crash outright (e.g. when you compile to PCode)
    - and are not needed at all, for proper threading in VB6

    The whole CodeBank-entry here was provided, to show a truly safe way with VB6-threading (avoiding the hacks).

    I've even addressed your "Thread-Form-issues" (with Project-Private UserControls) appropriately,
    by uploading a second example with support for that scenario (by encapsulating these thread-forms in their own Ax-Dll).

    ActiveX-Dlls are (generally) the proper place for "VB6-WorkerThread-code" (including Form-Definitions, when they shall run on their own thread)-
    And such AX-Dll-hosted WorkerThread-code will run properly, even when you use VB6-STA-based threading without AxExes.

    Ax-Dlls can easily be shipped in a \Bin\-folder beside your "normal StdExe-binary" in a true portable fashion (no registering needed).
    I have no clue, why so many in the VB-community are trying to avoid writing their own Ax-Dlls - there's absolutely no reason for that.

    HTH

    Olaf
    Dear Olaf,

    Thankyou for the reply.

    regards,
    JSVenu

  34. #34
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    Before displaying I was setting the thread to apartment model by modifying vbheader.
    I don't know how to explain you shouldn't use that module for ActiveX EXE. This module is intended for Standard EXE usage. ActiveX EXE supports threading out-of-box.

    The built-in threading-support (via COM-STAs and their automatic synchronizing)
    is (for me) comparable with a "Luxus-Van" ...

    It has a whole lot of comfort- and safety-features built-in (comparable to "ESP and ABS" + "airbags in every corner") -
    and it will cover 99% of all threading-scenarios (on normal roads).

    Whereas "the hacks" look like some "go-cart, kept together with a lot of duct-tape, always hoping the wheels don't fly off".

    If you really have to go "offroad" with your threading, then there's better suited (language-)vehicles out there.
    "The hack" is to use an external ActiveX OCX in order to use an internal user control i think. So i can use an external exe (or even the same exe) and use it for my threading scenarios by communication over a moniker. Moreover i don't need the registration in this case. You didn't show a real threading scenario as i wrote "threading-for-threading".

  35. #35
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    I don't know how to explain you shouldn't use that module for ActiveX EXE. This module is intended for Standard EXE usage. ActiveX EXE supports threading out-of-box.


    "The hack" is to use an external ActiveX OCX in order to use an internal user control i think. So i can use an external exe (or even the same exe) and use it for my threading scenarios by communication over a moniker. Moreover i don't need the registration in this case. You didn't show a real threading scenario as i wrote "threading-for-threading".
    Dear Trick,

    I was using vbheader only to set the thread created using CreateObject to apartment model only in order to load user control to the form in new thread since as you said it is the only way I know to access user control.Moreover once we set to apartment model by starting new thread we are unable to terminate the application properly since it is running in taskmanager even after closing the application.

    regards,
    JSVenu
    Last edited by jsvenu; Feb 27th, 2020 at 09:11 AM.

  36. #36
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    I was using vbheader only to set the thread created using CreateObject to apartment model only in order to load user control to the form in new thread since as you said it is the only way I know to access user control.Moreover once we set to apartment model by starting new thread we are unable to terminate the application properly since it is running in taskmanager even after closing the application.
    You shouldn't use the module for ActiveX EXE projects. ActiveX EXE already have the ability to create the threads. If you have a question about the usage/issues of the VbTrickThreading module in the Standard EXE then you can create a new thread. Please STOP hijack this thread and others! I won't answer you about VbTrickThreading in this thread anymore.

  37. #37
    Hyperactive Member
    Join Date
    Apr 2015
    Posts
    356

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    You shouldn't use the module for ActiveX EXE projects. ActiveX EXE already have the ability to create the threads. If you have a question about the usage/issues of the VbTrickThreading module in the Standard EXE then you can create a new thread. Please STOP hijack this thread and others! I won't answer you about VbTrickThreading in this thread anymore.
    Dear Trick,

    Ok Sorry for using vbheader in act exe for loading the same project user control in new thread form at runtime.

    Dear Olaf,

    Can you suggest any other way to load the same project existing user control in form of new thread at runtime without using vbheader for a activex exe application.

    regards,
    JSVenu

  38. #38

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    You didn't show a real threading scenario ...
    Maybe the new tutorial I've just posted into the codebank, is more helpful in understanding
    "how to handle ThreadObjects on COM-STAs" (without going too deep into what happens "underneath those STAs")...

    It's doing that in several steps (starting in Step0 with a "monolitic, nonthreaded Mandelbrot-rendering") -
    and evolves from there - over AxExe-based Steps - to AxDll-based STA-threading in the last Steps -
    ending with an "automatically filled Bin-Folder" for a regfree deployable, portable StdExe...

    http://www.vbforums.com/showthread.p...rot-Rendering)

    Olaf

  39. #39
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,673

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by Schmidt View Post
    Maybe the new tutorial I've just posted into the codebank, is more helpful in understanding
    "how to handle ThreadObjects on COM-STAs" (without going too deep into what happens "underneath those STAs")...

    It's doing that in several steps (starting in Step0 with a "monolitic, nonthreaded Mandelbrot-rendering") -
    and evolves from there - over AxExe-based Steps - to AxDll-based STA-threading in the last Steps -
    ending with an "automatically filled Bin-Folder" for a regfree deployable, portable StdExe...

    http://www.vbforums.com/showthread.p...rot-Rendering)

    Olaf
    Thank you for the example. I'll study your example.

    (without going too deep into what happens "underneath those STAs").
    I see you use CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream in the code to marshal an object reference between threads. You also use the message pumping to process the inter-threads communication. You use CreateThread API (free-threading) to create STA, manual synchronization (why cycle with sleep?), no marshaling for parameters. Literally the same things i used in my module.
    Note, I am not against this, but you just said the opposite.

  40. #40

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: VB6 Threaded Forms (simple Demo)

    Quote Originally Posted by The trick View Post
    I see you use CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream in the code to marshal an object reference between threads.
    Yes, of course - that's the usual (STA-conform) way for Proxy/Stub based Object-communication.

    Quote Originally Posted by The trick View Post
    You also use the message pumping to process the inter-threads communication.
    Yes, because every proper STA should have such a loop.

    Quote Originally Posted by The trick View Post
    You use CreateThread API (free-threading) to create STA,
    I have to, because this API is the only official way on a Win-OS, to create - well - a thread.
    Any language who wants to establish an STA (on a new thread), has to call this API at some point.

    And no - FreeThreading (in the real sense) implies also cross-thread-sharing of (global) variables -
    which I did not try to attempt (by hacking VB6 with regards to avoiding the built-in ThreadLocalStorage).

    Quote Originally Posted by The trick View Post
    ... (why cycle with sleep?)
    You mean implicitely - via DoEvents (and its "hidden Sleep(0) call")?

    Well, I've found - when developing/testing the (threaded) RPCserver-support in RC5 (decades ago) -
    that a final Sleep(0) call in the "pump-loop" was increasing performance under heavy concurrent load.

    So please read, what the MSDN has to say (about Sleep with Param 0) -
    or alternatively this discussion: https://stackoverflow.com/questions/...ance-of-sleep0

    Quote Originally Posted by The trick View Post
    Literally the same things i used in my module.
    I've never said, that *everything* in your threading-modules was "a hack".

    And in case you meant, I've "stolen something" - well, I didn't.
    In the new Demo, I've restricted myself to "only the things which are known to be safe to use"
    (especially when the whole STA-establishing stuff is encapsulated in a Dll, as in my example).

    Quote Originally Posted by The trick View Post
    Note, I am not against this, but you just said the opposite.
    I said, that the "interna of STAs" are not, what a User (of an STA-threaded solution) should "bother with".

    They are (black-box-like) hidden when the builtin AxExe-threading is used -
    and they are "hidden in a Dll-binary", when StdExes want to use the same STA-based threading
    (as shown in my example, or when using DirectCOM.dll from the RC5-package).

    Much more important (and time-consuming) with regards to threading,
    is it (for newcomers) to learn what I described in the Readme of the new Tutorial for Step1:
    - how to split-up (the load of) a monolithic routine into "concurrently runnable sub-parts".

    As soon as this Step1 is done, the rest is relatively easy (in most threading-scenarios) -
    aside from synchronization-efforts (which with that normal STA-based VB6-threading, are basically non-existent).

    Olaf

Page 1 of 2 12 LastLast

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