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