Threading and Scope

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tomb

    #1

    Threading and Scope

    Hi all.
    I can't find anything to explain how to prevent losing my object reference.

    I have a function that instantiates a document object, passes it to
    another class, then uses a thread for the other class to process data
    and populate the document with a report, then open the document. It all
    works great except the document opens and immediately closes.

    What I don't understand is this:
    If I Dim as New the document object in a function, then process the data
    and report in the other class without threading, the document opens and
    stays open even though the function is exited and the document object
    and other class go out of scope.
    But, if I do the same thing using threading, the document and class
    still go out of scope, but now I lose the document completely.

    Can anyone explain this? Better yet, how to avoid this? Or point me to
    a reference?

    So frustrating, so frustrating...

    Thanks,
    Tom
  • Samuel Shulman

    #2
    Re: Threading and Scope

    It Word document?


    "tomb" <tomb@technetce nter.comwrote in message
    news:0Zqtg.2233 0$Ud4.14111@big news1.bellsouth .net...
    Hi all.
    I can't find anything to explain how to prevent losing my object
    reference.
    >
    I have a function that instantiates a document object, passes it to
    another class, then uses a thread for the other class to process data and
    populate the document with a report, then open the document. It all works
    great except the document opens and immediately closes.
    What I don't understand is this:
    If I Dim as New the document object in a function, then process the data
    and report in the other class without threading, the document opens and
    stays open even though the function is exited and the document object and
    other class go out of scope.
    But, if I do the same thing using threading, the document and class still
    go out of scope, but now I lose the document completely.
    >
    Can anyone explain this? Better yet, how to avoid this? Or point me to a
    reference?
    >
    So frustrating, so frustrating...
    >
    Thanks,
    Tom

    Comment

    • tomb

      #3
      Re: Threading and Scope

      It's a generic document form with vb.net.

      T

      Samuel Shulman wrote:
      >It Word document?
      >
      >
      >"tomb" <tomb@technetce nter.comwrote in message
      >news:0Zqtg.223 30$Ud4.14111@bi gnews1.bellsout h.net...
      >
      >
      >>Hi all.
      >>I can't find anything to explain how to prevent losing my object
      >>reference.
      >>
      >>I have a function that instantiates a document object, passes it to
      >>another class, then uses a thread for the other class to process data and
      >>populate the document with a report, then open the document. It all works
      >>great except the document opens and immediately closes.
      >>What I don't understand is this:
      >>If I Dim as New the document object in a function, then process the data
      >>and report in the other class without threading, the document opens and
      >>stays open even though the function is exited and the document object and
      >>other class go out of scope.
      >>But, if I do the same thing using threading, the document and class still
      >>go out of scope, but now I lose the document completely.
      >>
      >>Can anyone explain this? Better yet, how to avoid this? Or point me to a
      >>reference?
      >>
      >>So frustrating, so frustrating...
      >>
      >>Thanks,
      >>Tom
      >>
      >>
      >
      >
      >
      >

      Comment

      • Branco Medeiros

        #4
        Re: Threading and Scope


        tomb wrote:
        <snip>
        I have a function that instantiates a document object, passes it to
        another class, then uses a thread for the other class to process data
        and populate the document with a report, then open the document. It all
        works great except the document opens and immediately closes.
        <snip>

        Since the form is created in another thread, as soon as the threaded
        function finishes, the thread vanishes and the form 'dies'.

        One possible solution would be to open the form with ShowDialog(), but
        notice that this will show the form modally and may prevent your
        function from working.

        HTH,

        Branco

        Comment

        • tomb

          #5
          Re: Threading and Scope

          Actually, the form is declared and instantiated in my non-threaded
          function, then passed to the other class, that then is called in a
          thread to do the processing. But I appreciate the attempt.

          T

          Branco Medeiros wrote:
          >tomb wrote:
          ><snip>
          >
          >
          >>I have a function that instantiates a document object, passes it to
          >>another class, then uses a thread for the other class to process data
          >>and populate the document with a report, then open the document. It all
          >>works great except the document opens and immediately closes.
          >>
          >>
          ><snip>
          >
          >Since the form is created in another thread, as soon as the threaded
          >function finishes, the thread vanishes and the form 'dies'.
          >
          >One possible solution would be to open the form with ShowDialog(), but
          >notice that this will show the form modally and may prevent your
          >function from working.
          >
          >HTH,
          >
          >Branco
          >
          >
          >

          Comment

          • Branco Medeiros

            #6
            Re: Threading and Scope


            tomb wrote:
            Actually, the form is declared and instantiated in my non-threaded
            function, then passed to the other class, that then is called in a
            thread to do the processing. But I appreciate the attempt.
            It would help if you posted some code, then... =)

            Regards,

            Branco.

            Comment

            • Michael D. Ober

              #7
              Re: Threading and Scope

              You need to pass a reference to your document to your thread. Otherwise,
              when the function that opens the document terminates, your document goes out
              of scope and the GC reclaims it.

              Mike Ober.

              "tomb" <tomb@technetce nter.comwrote in message
              news:0Zqtg.2233 0$Ud4.14111@big news1.bellsouth .net...
              Hi all.
              I can't find anything to explain how to prevent losing my object
              reference.
              >
              I have a function that instantiates a document object, passes it to
              another class, then uses a thread for the other class to process data
              and populate the document with a report, then open the document. It all
              works great except the document opens and immediately closes.
              >
              What I don't understand is this:
              If I Dim as New the document object in a function, then process the data
              and report in the other class without threading, the document opens and
              stays open even though the function is exited and the document object
              and other class go out of scope.
              But, if I do the same thing using threading, the document and class
              still go out of scope, but now I lose the document completely.
              >
              Can anyone explain this? Better yet, how to avoid this? Or point me to
              a reference?
              >
              So frustrating, so frustrating...
              >
              Thanks,
              Tom
              >


              Comment

              Working...