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 recognize that this is in the collected thread?).
This is a sample:
using System;
using System.Threading;
namespace Test {
public class Test {
static ManualResetEvent evnt = new ManualResetEvent(false);
static void CreateThread()
{
Thread t = new Thread(new ThreadStart(ThreadMethod));
t.IsBackground = true;
t.Start();
}
static void ThreadMethod()
{
Console.WriteLine("Thread started");
evnt.Set();
while (true)
{
Console.WriteLine("From thread.");
Thread.Sleep(1000);
}
}
[STAThread]
public static void Main()
{
CreateThread();
evnt.WaitOne();
Console.WriteLine("press enter to GC");
Console.ReadLine();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("press enter for exit");
Console.ReadLine();
}
}
}
The thread continues to run even after GC.Collect? Why? What holds a
reference?
And why TaskManager shows 6 threads running (before and after the
GC.Collect call) and 5 after that? I can count to 3 - the main one, GC
thread and my worker thread. What are the others?
Thanks
Sunny 5 2236
"Sunny" <su***@newsgroups.nospam> wrote in message
news:e7**************@tk2msftngp13.phx.gbl... 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.
Nothing in particular.
Is it garbage collected (I assume yes, as it is a regular object and there is no reference to it).
No. The CLR (in effect, though perhaps not literally) keeps a pointer to
each active thread. Until the thread exits, it will not be collected.
Hi Sunny
Threads are considered GC Roots by the runtime, so they are not collected (otherwise you'd have to have a strong reference to every running thread).
Hope that helps
-Chris
-------------------- 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 recognize that this is in the collected thread?).
This is a sample:
using System; using System.Threading;
namespace Test {
public class Test {
static ManualResetEvent evnt = new ManualResetEvent(false);
static void CreateThread() { Thread t = new Thread(new ThreadStart(ThreadMethod)); t.IsBackground = true; t.Start(); }
static void ThreadMethod() { Console.WriteLine("Thread started"); evnt.Set(); while (true) { Console.WriteLine("From thread."); Thread.Sleep(1000); } }
[STAThread] public static void Main() { CreateThread(); evnt.WaitOne(); Console.WriteLine("press enter to GC"); Console.ReadLine(); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("press enter for exit"); Console.ReadLine(); } } }
The thread continues to run even after GC.Collect? Why? What holds a reference?
And why TaskManager shows 6 threads running (before and after the GC.Collect call) and 5 after that? I can count to 3 - the main one, GC thread and my worker thread. What are the others?
Thanks Sunny
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
Thanks Mike,
exactly what I see, but I was not sure if this is the expected
behaviour.
Sunny
In article <er**************@TK2MSFTNGP10.phx.gbl>, ms*************@hotmail.com says... "Sunny" <su***@newsgroups.nospam> wrote in message news:e7**************@tk2msftngp13.phx.gbl... 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.
Nothing in particular.
Is it garbage collected (I assume yes, as it is a regular object and there is no reference to it).
No. The CLR (in effect, though perhaps not literally) keeps a pointer to each active thread. Until the thread exits, it will not be collected.
Thanks Chris,
can you provide a link for more info, it is interesting subject :). I.e.
I'm interested when they are considered collectable, etc.
Sunny
In article <9s**************@cpmsftngxa10.phx.gbl>, cl***@online.microsoft.com says... Hi Sunny
Threads are considered GC Roots by the runtime, so they are not collected (otherwise you'd have to have a strong reference to every running thread).
Hope that helps -Chris
-------------------- 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 recognize that this is in the collected thread?).
This is a sample:
using System; using System.Threading;
namespace Test {
public class Test {
static ManualResetEvent evnt = new ManualResetEvent(false);
static void CreateThread() { Thread t = new Thread(new ThreadStart(ThreadMethod)); t.IsBackground = true; t.Start(); }
static void ThreadMethod() { Console.WriteLine("Thread started"); evnt.Set(); while (true) { Console.WriteLine("From thread."); Thread.Sleep(1000); } }
[STAThread] public static void Main() { CreateThread(); evnt.WaitOne(); Console.WriteLine("press enter to GC"); Console.ReadLine(); GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("press enter for exit"); Console.ReadLine(); } } }
The thread continues to run even after GC.Collect? Why? What holds a reference?
And why TaskManager shows 6 threads running (before and after the GC.Collect call) and 5 after that? I can count to 3 - the main one, GC thread and my worker thread. What are the others?
Thanks Sunny
Hi Sunny
As long as the thread is running, it will not be collected. This is because a running thread is an implicit GC root, with a thread of execution. Without that root, your thread couldn't
run to completion if it went out of scope. Take a look in MSDN for articles on threading and GC for more information.
-Chris
-------------------- From: Sunny <su***@newsgroups.nospam> Subject: RE: threads and garbage collection Date: Tue, 27 Jul 2004 17:48:56 -0500 References: <e7**************@tk2msftngp13.phx.gbl> <9s**************@cpmsftngxa10.phx.gbl> Organization: Iceberg Wireless LLC MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit X-Newsreader: MicroPlanet Gravity v2.60 Message-ID: <uV*************@tk2msftngp13.phx.gbl> Newsgroups: microsoft.public.dotnet.languages.csharp NNTP-Posting-Host: 216.17.90.91 Lines: 1 Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFT NGP08.phx.gbl!tk2msftngp13.phx.gbl Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:261350 X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
Thanks Chris,
can you provide a link for more info, it is interesting subject :). I.e. I'm interested when they are considered collectable, etc.
Sunny
In article <9s**************@cpmsftngxa10.phx.gbl>, cl***@online.microsoft.com says... Hi Sunny
Threads are considered GC Roots by the runtime, so they are not collected (otherwise you'd have to have a strong reference to every running thread).
Hope that helps -Chris
-------------------- > >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 recognize that this is in the collected thread?). > >This is a sample: > >using System; >using System.Threading; > >namespace Test { > >public class Test { > > static ManualResetEvent evnt = new ManualResetEvent(false); > > static void CreateThread() > { > Thread t = new Thread(new ThreadStart(ThreadMethod)); > t.IsBackground = true; > t.Start(); > } > > static void ThreadMethod() > { > Console.WriteLine("Thread started"); > evnt.Set(); > while (true) > { > Console.WriteLine("From thread."); > Thread.Sleep(1000); > } > } > > [STAThread] > public static void Main() > { > CreateThread(); > evnt.WaitOne(); > Console.WriteLine("press enter to GC"); > Console.ReadLine(); > GC.Collect(); > GC.WaitForPendingFinalizers(); > Console.WriteLine("press enter for exit"); > Console.ReadLine(); > } >} >} > > >The thread continues to run even after GC.Collect? Why? What holds a >reference? > >And why TaskManager shows 6 threads running (before and after the >GC.Collect call) and 5 after that? I can count to 3 - the main one, GC >thread and my worker thread. What are the others? > >Thanks >Sunny >
--
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Thomas Smith |
last post by:
I am building a binding to a runtime engine that was written in C++.
I have no problems setting up the mapping from object calls to the native
implementation, and storing reference to those...
|
by: Ganesh |
last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a
process without have access to the process code.
regards
Ganesh
|
by: Rick |
last post by:
Hi,
My question is.. if Lisp, a 40 year old language supports garbage
collection, why didn't the authors of C++ choose garbage collection for
this language? Are there fundamental reasons behind...
|
by: Ville Voipio |
last post by:
I would need to make some high-reliability software
running on Linux in an embedded system. Performance
(or lack of it) is not an issue, reliability is.
The piece of software is rather simple,...
|
by: Bob lazarchik |
last post by:
Hello:
We are considering developing a time critical system in C#. Our tool used in
Semiconductor production and we need to be able to take meaurements at
precise 10.0 ms intervals( 1000...
| |
by: Martin Maat |
last post by:
I am puzzled.
I have this object that uses a thread. The thread is encapsulated by the
object, the object has Start and Stop methods to enable the client to start
or stop the thread.
I found...
|
by: mike2036 |
last post by:
For some reason it appears that garbage collection is releasing an object
that I'm still using. The object is declared in a module and instantiated
within a class that is in turn instantiated by...
|
by: Johnny E. Jensen |
last post by:
Hellow
I'am not sure what to think about the Garbage Collector.
I have a Class OutlookObject, It have two private variables.
Private Microsoft.Office.Interop.Outlook.Application _Application =...
|
by: pushpakulkar |
last post by:
Hi all,
Is garbage collection possible in C++. It doesn't come as part of
language support. Is there any specific reason for the same due to the
way the language is designed. Or it is...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |