Worker thread exits when from loads. Why?

D

DaTurk

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
 
J

Jayme.Pechan

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.
 
D

DaTurk

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.
 
P

Peter Duniho

DaTurk said:
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 BackgroundWorker 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
 
I

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

Hi,

DaTurk said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top