dcsimg
Results 1 to 29 of 29

Thread: VB6 Threaded Forms (simple Demo)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,570

    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
    Frenzied Member
    Join Date
    Jun 2012
    Posts
    1,385

    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
    4,570

    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
    4,570

    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
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,488

    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
    4,570

    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
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,488

    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
    4,570

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,488

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    4,570

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,488

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    Re: VB6 Threaded Forms (simple Demo)

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

  25. #25
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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
    Addicted Member
    Join Date
    Jan 2020
    Posts
    232

    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
    Addicted Member
    Join Date
    Apr 2015
    Posts
    246

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width