473,657 Members | 2,405 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threads and scope

I use the class below as a base class to make object that execute in
their own thread. If I create a derived class and instantiate an
instance of that class, what happens to the thread if the instance goes
out of scope? Will the thread continue to run? Does the thread run
independently of the object? Does the thread keep the object alive
even though my reference to it goes out of scope?

Public MustInherit Class ThreadWrapper

'Our thread object
Public ReadOnly Thread As System.Threadin g.Thread

Public Sub New()
'Create the thread
Me.Thread = New System.Threadin g.Thread(Addres sOf
Me.StartThread)
End Sub

'Starts execution of the thread
Public Overridable Sub Start()
Me.Thread.Start ()
End Sub

'Aborts the thread. this sub can be overridden to kill the thread
more gracefully
Public Overridable Sub [Stop]()
Me.Thread.Abort ()
End Sub

'Property to indicate the thread is completed
Private m_IsCompleted As Boolean
Public ReadOnly Property IsCompleted() As Boolean
Get
Return m_IsCompleted
End Get
End Property

'When the thread is started, this is the sub that is called, it in
turn calls the Execute method which
'derived classes must override.
Private Sub StartThread()
m_IsCompleted = False
Execute()
m_IsCompleted = True
End Sub

'This is the sub that performs the "work" of the thread. It must
be overridden in the derived class
Protected MustOverride Sub Execute()

End Class

Nov 21 '05 #1
1 2169
If I create a derived class and instantiate an
instance of that class, what happens to the thread if the instance goes
out of scope? Will the thread continue to run?
Yes, the thread terminates when the method you instantiated it with
(StartThread) exits.

Does the thread run independently of the object?
Yes

Does the thread keep the object alive
even though my reference to it goes out of scope?


I don't think the thread itself necessarily keeps the object alive,
but the code that the thread runs will certainly keep it alive as long
as it's needed.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
537
by: Bart Nessux | last post by:
Could someone explain the concept of threads and how I might use them in Python? I was a math major, not a CS major (theory instead of practice). Most of my programming knowledge has grown out of system administration and shell scripting, not professional software development. How could I make this script threaded, and why should I, how would it benefit me? The script already takes up 100% CPU resources, what would using threads gain for...
9
2544
by: Nicolas Fleury | last post by:
Hi everyone, I just discovered, correct me if I'm wrong, that as long as a threading.Thread is running, its destructor will not be called, because it is referring to himself. So if I have something like: class MyThread(threading.Thread): def __init__(self): self.cancelEvent = threading.Event() threading.Thread.__init__(self) def __del__(self):
2
1584
by: yoda | last post by:
Recently I read Charming Python: Implementing Weightless Threads (http://www-128.ibm.com/developerworks/linux/library/l-pythrd.html) by David D. I'm not an authority on threading architectures so I'd like to ask the following: 1)What is the difference (in terms of performance, scalability,) between microthreads and "system" threads? 2)If microthreads really are superior then why aren't they the standard
5
2252
by: Sunny | last post by:
Hi all, Does any one knows, if I create a thread in a method and start some processing in it, what happens when this thread object goes out of scope. Is it garbage collected (I assume yes, as it is a regular object and there is no reference to it). If it is, what happens with the executed code inside the thread? What kind of exceptions can I receive (if any) and how to capture them (AppDomain.UnhandledException may help, but how do I...
21
1624
by: Doug Thews | last post by:
I was noticing that when I run a multi-threaded app from within the IDE and then I close the main form (which spun off all of the threads), that the Stop/Pause VCR buttons in the debugger are still available & working. It's like closing the form did not actually cancel the threads. From my earlier discussions on the newsgroup, I know how people already feel about Thread.Abort(), so I was wondering what I need to do to "clean up" when...
2
2094
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these instances to process each incoming HTTP request that is received by the application. The particular HttpApplication instance assigned is responsible for managing the entire lifetime of the request and is reused only after the request has been...
8
1984
by: raghu | last post by:
Hello Everyone I am working on C platform for a project in that project I need to maintain 'n' number of states where n is a dynamic value simultaneously. But I should not use any kind of threads since number of threads allowed, in the project, already reached and I can't predict how many threads to be maintained in this case. Can you please pull me out of this problem. Thanks in adv
1
4133
by: chunghorng_lung | last post by:
Hi All, I have a question on the scope of variables for threads. The program has a main thread which creates a few worker threads. The main thread can access another class stored in another file, however, the worker threads can't. I got link errors for the code inside of the worker threads. The following contains code segments. Multi_Queue is a class and getXXX() is a public member function inside of Multi_Queue which is in...
1
3470
by: Chris Roth | last post by:
I've been working with boost::threads to do some multithreading in my code and have run into some questions that I haven't been able to find answers to. I'll include some sample code to illustrate my questions. #include <boost/thread/thread.hpp> #include <iostream> using namespace std;
0
970
by: bennett | last post by:
Hi All, I am making a web service that needs make several calls to different websites and format the website responses into the same format using XSLT. Currently I am just using an array of threads that starts a class with a complete/done event handler. When each feed has finished it increments a counter so I can see how many thread were fired off and how many completed. The problem is i don't want any of the threads to take longer...
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.