Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Worker thread exits when from loads.  Why?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DaTurk - 31 Aug 2007 17:01 GMT
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 - 31 Aug 2007 17:09 GMT
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.
DaTurk - 31 Aug 2007 17:28 GMT
> 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.
Peter Duniho - 31 Aug 2007 17:53 GMT
> 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
[quoted text clipped - 5 lines]
> 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
Ignacio Machin ( .NET/ C# MVP ) - 31 Aug 2007 18:53 GMT
Hi,

> Hi,
>
[quoted text clipped - 3 lines]
> 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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.