Worker thread exits when from loads. Why?

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

    #1

    Worker thread exits when from loads. Why?

    Hi,

    I have this windows app that I'm working on. When it loads it
    instantiates this object which creates a thread to read data and send
    it to the form. My problem is, is that I want to keep this thread
    around for the lifetime of the object, reading and sending data to the
    from, but it seems to exit when the form completed loading.

    Why is it doing this? And is there a way I can keep it around after
    the form loads, without the form actually knowing anything about the
    thread.

    THanks

  • Jayme.Pechan

    #2
    Re: Worker thread exits when from loads. Why?

    I'm assuming this is obvious but make sure your thread variable stays in
    scope after initialization. If the form stays visible, you can make the
    object that creates the thread an instance variable. Make sure the actual
    thread variable within the object stays in scope too.


    Comment

    • DaTurk

      #3
      Re: Worker thread exits when from loads. Why?

      On Aug 31, 11:09 am, "Jayme.Pech an" <ja...@pechan.u swrote:
      I'm assuming this is obvious but make sure your thread variable stays in
      scope after initialization. If the form stays visible, you can make the
      object that creates the thread an instance variable. Make sure the actual
      thread variable within the object stays in scope too.
      Yes thank you. It is staying in scope. The thread variable is a
      class scop instance variable. And the class is also a class scope
      instance variable in the form.

      Comment

      • Peter Duniho

        #4
        Re: Worker thread exits when from loads. Why?

        DaTurk wrote:
        I have this windows app that I'm working on. When it loads it
        instantiates this object which creates a thread to read data and send
        it to the form. My problem is, is that I want to keep this thread
        around for the lifetime of the object, reading and sending data to the
        from, but it seems to exit when the form completed loading.
        >
        Why is it doing this? And is there a way I can keep it around after
        the form loads, without the form actually knowing anything about the
        thread.
        There is no need to keep the Thread instance referenced. A thread isn't
        going to go away until the thread procedure (that is, the method that's
        the entry point for the thread) exits. So the other reply is off-base
        in implying that this is required.

        On the other hand, the converse is also true. A thread _will_ go away,
        always, if the thread procedure exits. There's nothing you can do about
        this. It's how threads work.

        As far as your specific question goes, it's not clear what you're trying
        to do or what code you're using. The simple answer is "don't let the
        thread's thread procedure exit". You don't actually describe what "this
        object" is, or how it's implemented, but that's the basic answer.

        On the other hand, the more general idea of what you're describing is
        probably more appropriately solved using the existing thread pool.
        Commonly the BackgroundWorke r class is used for this, though you can use
        the thread pool directly as well if you like.

        With more details about your specific scenario, you may receive more
        specific and useful advice.

        Pete

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: Worker thread exits when from loads. Why?

          Hi,

          "DaTurk" <mmagdits@hotma il.comwrote in message
          news:1188576078 .934456.24350@y 42g2000hsy.goog legroups.com...
          Hi,
          >
          I have this windows app that I'm working on. When it loads it
          instantiates this object which creates a thread to read data and send
          it to the form. My problem is, is that I want to keep this thread
          around for the lifetime of the object, reading and sending data to the
          from, but it seems to exit when the form completed loading.
          The thread should ends when the method ends. It has no connection with the
          form loading process (at least from your post)

          Why is it doing this? And is there a way I can keep it around after
          the form loads, without the form actually knowing anything about the
          thread.
          Why would you want to have a form without doing nothing?


          Comment

          Working...