Announcement

Collapse
No announcement yet.

Passing object to worker thread...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing object to worker thread...

    Hello,

    Thanks to this forum, I've managed to make great progress in using a 3rd party [motion control] library. However, there's one thing I can't figure out, and it has to do with setting up a worker thread to periodically poll for positional data. I've done this before, but never in COM, so here's some information that may help you guide me in the right direction.

    First, I've set up 'motor' objects in my main thread, and this is how I've been talking to each axis [with great success]. Now, to use a worker thread, it seems to me that I have to make it aware of these objects, but I don't know how. So, how does one pass an object to a thread?

    -CMR

  • #2
    See: http://www.powerbasic.com/support/pb...ghlight=objptr
    Forum: http://www.jose.it-berater.org/smfforum/index.php

    Comment


    • #3
      OBJPTR gets me a pointer, but then what?

      José,

      Ok, I've read that thread, and it's starting to make some sense. In my case, however, I have three objects of which the thread must be made aware. I'm assuming I do this by creating a UDT with three members, and then pass a pointer to this UDT. Then, in the thread, I create a UDT based on this pointer, and thus now I have pointers to these three objects. Now what? I don't fully understand what to do with CoInitializeEx and CoUninitialize, either.

      -CMR

      Comment


      • #4
        In my case, however, I have three objects of which the thread must be made aware. I'm assuming I do this by creating a UDT with three members, and then pass a pointer to this UDT. Then, in the thread, I create a UDT based on this pointer, and thus now I have pointers to these three objects. Now what?
        You will need to convert these pointers to object variables. Would not be easier to make the three object variables global?

        I don't fully understand what to do with CoInitializeEx and CoUninitialize, either.
        You need to call CoInitialize/Ex if you intend to create instances of objects in the thread. But calling it, the thread enters in a new STA (Single Threaded Apartment) and then to share interface pointers they have to be marshaled by using the COM API functions CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream or placing them in the Global InterfaceTable.

        See: http://msdn.microsoft.com/en-us/magazine/cc302324.aspx
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #5
          Gee, who knows...eh... memorizes all that stuff
          So here we are, this is the end.
          But all that dies, is born again.
          - From The Ashes (In This Moment)

          Comment


          • #6
            More CoInitialize questions...

            Given the wording of the different documents, and with no talk about CoInitialize or CoInitializeEX in the PB help file, I'm confused one these points...

            Can anyone confirm when, if ever, PB calls CoInitializeEx on our behalf inside the PB COM framework? I am guessing that it does call it on our behalf, since I don't see it used in any of the samples and it does need to get called at some level.

            Important: When/If I do need to call CoInitializeEx, do I call it once per thread that creates instances of objects, or do I have to call it once inside of each object per thread?

            If/when PB does call CoInitializeEx on our behalf, which mode does it put it into?

            If it calls CoInitializeEx on our behalf and defaults to COINIT_APARTMENTTHREADED, does this imply that PB is not compatible with COINIT_MULTITHREADED?

            Thank you very much to anyone who can clarify any of this!

            Regards,
            Colin Schmidt

            Comment


            • #7
              CoInitializeEx is called on the main thread in all PB-compiled applications, needed or not.. at least it is always imported, Can't really tell if it's acutally called, but since COM works without an explicit call, it must be called at least when one of the statements requiring it is used somewhere in the module.

              AFAIK the options used when calling are a state secret.

              But you could try calling CoUnitializeEx after program startup, then calling CoInitializeEx yourself with the desired options and see what happens.

              MCM
              Michael Mattias
              Tal Systems Inc. (retired)
              Racine WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                More testing...

                Hi Michael,

                I did call CoInitializeEx myself and was surprised to see the return of zero, which indicates that the function had not previously been called. That is what lead me down the road to posted my questions.

                Giving it more thought now with your reply - I considered that I was doing so in a Windows Service - I tested it in a skeleton PBMAIN program and found that it was previously called.

                Thus, the lesson here is that when you are creating a Windows Service in PB and using COM objects, you do need to call CoInitializeEx yourself.

                On a similar note for other readers of this thread: if you have other threads created for you by a non-PB complied library, those threads will not necessarily have called CoInitializeEx.

                Now, a remaining question - since PB does call CoInitializeEx for us, it thus chooses one of the two modes. If I initialize my Windows Service using COINIT_MULTITHREADED and then create instances of objects I created in PB could I have issues with the PB runtime? If I don't hear from PB staff in this post I'll email support and reply back here with the answer.

                Regards,
                Colin

                Comment


                • #9
                  >If I don't hear from PB staff in this post ...

                  Somewhere this venue is explicitly disclaimed to be any kind of official support channel - it is for *peer* support only . If you need/want "official" send your request directly to PB support.

                  MCM
                  Michael Mattias
                  Tal Systems Inc. (retired)
                  Racine WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    PowerBASIC calls CoInitializeEx in the main thread and in each PowerBASIC created thread. You may call CoInitializeEx with %COINIT_MULTITHREADED, but you must first call CoUninitialize to uninitialize the CoInitializeEx with %COINIT_APARTMENTTHREADED that PowerBASIC already called first. This must be done before any COM calls are made in the thread. Then you must re-initialize the thread before exiting with a call to CoInitializeEx with %COINIT_APARTMENTTHREADED, so that PB can call CoUninitialize when the thread exits.
                    Sincerely,

                    Steve Rossell
                    PowerBASIC Staff

                    Comment


                    • #11
                      Hi Steve,

                      What you are saying is:

                      Code:
                      [hidden] CoInitializeEx %COINIT_APARTMENTTHREADED
                      CoUnInitialize
                      CoInitializeEx %COINIT_MULTITHREADED
                      CoInitializeEx %COINIT_APARTMENTTHREADED
                      [hidden] CoUnInitialize
                      But as CoInitializeEx and CoUnInitialize must be paired, then it must be:

                      Code:
                      [hidden] CoInitializeEx %COINIT_APARTMENTTHREADED
                      CoUnInitialize
                      CoInitializeEx %COINIT_MULTITHREADED
                      CoUnInitialize
                      CoInitializeEx %COINIT_APARTMENTTHREADED
                      [hidden] CoUnInitialize
                      or:

                      Code:
                      [hidden] CoInitializeEx %COINIT_APARTMENTTHREADED
                      CoUnInitialize
                      CoInitializeEx %COINIT_MULTITHREADED
                      [hidden] CoUnInitialize
                      Forum: http://www.jose.it-berater.org/smfforum/index.php

                      Comment


                      • #12
                        Now I am getting lost????
                        Over in http://www.powerbasic.com/support/pb...ad.php?t=39000

                        Bob himself stated
                        Actually, you don't need to call CoInitialize at all. PowerBASIC 9 does it for you at the start of every thread.
                        Engineer's Motto: If it aint broke take it apart and fix it

                        "If at 1st you don't succeed... call it version 1.0"

                        "Half of Programming is coding"....."The other 90% is DEBUGGING"

                        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                        Comment


                        • #13
                          This one:
                          Code:
                          [hidden] CoInitializeEx %COINIT_APARTMENTTHREADED
                          CoUnInitialize
                          CoInitializeEx %COINIT_MULTITHREADED
                          CoUnInitialize
                          CoInitializeEx %COINIT_APARTMENTTHREADED
                          [hidden] CoUnInitialize
                          Sincerely,

                          Steve Rossell
                          PowerBASIC Staff

                          Comment


                          • #14
                            Now I am getting lost????
                            You don't need to call CoInitialize/Ex unless you need to change the apartment model.
                            Forum: http://www.jose.it-berater.org/smfforum/index.php

                            Comment


                            • #15
                              Originally posted by José Roca View Post
                              You don't need to call CoInitialize/Ex unless you need to change the apartment model.
                              True.
                              Sincerely,

                              Steve Rossell
                              PowerBASIC Staff

                              Comment


                              • #16
                                Well, with this answer disposed of....

                                I'll take "obscure technical references" for TWO hundred, please.
                                Michael Mattias
                                Tal Systems Inc. (retired)
                                Racine WI USA
                                [email protected]
                                http://www.talsystems.com

                                Comment


                                • #17
                                  It's just like using Brakets - they have to be paired.

                                  (|)()(|)
                                  --Theo Gottwald
                                  ------------------------------------------------
                                  76706 Dettenheim * Germany * [email protected]
                                  ------------------------------------------------
                                  Joses Forum * Theo's Link Site * IT-Berater.org

                                  Comment


                                  • #18
                                    What motion controller are you using? There may be an easier way :coffee4:
                                    Engineer's Motto: If it aint broke take it apart and fix it

                                    "If at 1st you don't succeed... call it version 1.0"

                                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                    Comment

                                    Working...
                                    X