Threading

A

AdrianJMartin

Hi all,

I have a need for a STA thread from asp.net. I can create the thread and it
runs fine. But when it is finished, the thread still 'hangs' arround.
Visible only to the debugger.....
I get the "The thread has exited with code 0 (0x12fc)." in the debugger
each time the thread seemly end, but its still there in the thread window.
GC.Collect() does not get rid of them either, something must be holding a
reference to the threads.........


a trivial example that demonstrates this

public void Work()
{
Thread.Sleep( 5000 ) ;
System.Diagnostics.Debug.WriteLine("Done!");
}

private void Button1_Click(object sender, System.EventArgs e)
{
ThreadStart ts = new ThreadStart( Work ) ;
Thread t = new Thread( ts );
t.Name = "test";
t.Start();
t.Join();
Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
t = null ;
GC.Collect();
}


Is there anyway to clean up the Thread?
 
K

Kevin Spencer

GC.Collect() doesn't clean everything up. You might want to wait around and
see if it gets cleaned up eventually. Otherwise, unless you have some reason
to think that it won't get cleaned up (such as interop being used in the
thread, and you're not sure that the Interop classes are being disposed
correctly), just trust the Framework.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
B

Brian Gideon

If the execution of Button1_Click makes it past the call to Thread.Join
then the thread definitely terminated. The debugger may not be
refreshing the thread window correctly.
 
A

Alvin Bruney - ASP.NET MVP

I see no reason for STA thread based on the example you provided. Care to
elaborate? STA threads are for COM based process by the way.

sorry, scratch that. I realized you were simply using incorrect terminology.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
A

AdrianJMartin

I DO need an STA thread, I'm creating an invisible window that hosts mshtml
and axwebrowser. I'm dynamically creating a bitmap using html. I need some
consistancy across platforms, I have this code working fine. But I have to
create a thread(STA) to host it.

In my testing i've noticed that the thread hangs arround in the debugger.
Even stripping my component out and creating the do almost nothing thread
example that i posted leaves this 'orphaned' thread. Regardless of the
apartment state.

I can't use the threadpool as the those threads are always MTA.


Alvin Bruney - ASP.NET MVP said:
I see no reason for STA thread based on the example you provided. Care to
elaborate? STA threads are for COM based process by the way.

sorry, scratch that. I realized you were simply using incorrect
terminology.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------



AdrianJMartin said:
Hi all,

I have a need for a STA thread from asp.net. I can create the thread and it
runs fine. But when it is finished, the thread still 'hangs' arround.
Visible only to the debugger.....
I get the "The thread has exited with code 0 (0x12fc)." in the debugger
each time the thread seemly end, but its still there in the thread
window.
GC.Collect() does not get rid of them either, something must be holding a
reference to the threads.........


a trivial example that demonstrates this

public void Work()
{
Thread.Sleep( 5000 ) ;
System.Diagnostics.Debug.WriteLine("Done!");
}

private void Button1_Click(object sender, System.EventArgs e)
{
ThreadStart ts = new ThreadStart( Work ) ;
Thread t = new Thread( ts );
t.Name = "test";
t.Start();
t.Join();
Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
t = null ;
GC.Collect();
}


Is there anyway to clean up the Thread?
 
S

Steven Cheng[MSFT]

Hi Adrian,

If the hang or unreleased thread you watched is the underlying operating
system thread, then that should be reasonable since regardless of the
thread operation (start, end,.....), the CLR's unmanaged code has structure
to hold reference to operating system thread. And so there is no definite
mapping between the underlying operating system thread to the managed CLR
thread instance..... Sometimes, our CLR t hread's work has finished, the
underlying CLR structure may still hold the OS thread. Or other times event
the OS thread has destroyed, the CLR's thread reference still refer to a
managed thread object (for keepting some thread information....)...

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "AdrianJMartin" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Threading
| Date: Mon, 19 Dec 2005 09:43:27 -0000
| Lines: 77
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:365658
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I DO need an STA thread, I'm creating an invisible window that hosts
mshtml
| and axwebrowser. I'm dynamically creating a bitmap using html. I need
some
| consistancy across platforms, I have this code working fine. But I have
to
| create a thread(STA) to host it.
|
| In my testing i've noticed that the thread hangs arround in the debugger.
| Even stripping my component out and creating the do almost nothing thread
| example that i posted leaves this 'orphaned' thread. Regardless of the
| apartment state.
|
| I can't use the threadpool as the those threads are always MTA.
|
|
| "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc> wrote in message
| | >I see no reason for STA thread based on the example you provided. Care to
| > elaborate? STA threads are for COM based process by the way.
| >
| > sorry, scratch that. I realized you were simply using incorrect
| > terminology.
| >
| > --
| > Regards,
| > Alvin Bruney [MVP ASP.NET]
| >
| > [Shameless Author plug]
| > The Microsoft Office Web Components Black Book with .NET
| > Now Available @ www.lulu.com/owc
| > Forth-coming VSTO.NET - Wrox/Wiley 2006
| > -------------------------------------------------------
| >
| >
| >
| > | >> Hi all,
| >>
| >> I have a need for a STA thread from asp.net. I can create the thread
and
| > it
| >> runs fine. But when it is finished, the thread still 'hangs' arround.
| >> Visible only to the debugger.....
| >> I get the "The thread has exited with code 0 (0x12fc)." in the
debugger
| >> each time the thread seemly end, but its still there in the thread
| >> window.
| >> GC.Collect() does not get rid of them either, something must be
holding a
| >> reference to the threads.........
| >>
| >>
| >> a trivial example that demonstrates this
| >>
| >> public void Work()
| >> {
| >> Thread.Sleep( 5000 ) ;
| >> System.Diagnostics.Debug.WriteLine("Done!");
| >> }
| >>
| >> private void Button1_Click(object sender, System.EventArgs e)
| >> {
| >> ThreadStart ts = new ThreadStart( Work ) ;
| >> Thread t = new Thread( ts );
| >> t.Name = "test";
| >> t.Start();
| >> t.Join();
| >> Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
| >> t = null ;
| >> GC.Collect();
| >> }
| >>
| >>
| >> Is there anyway to clean up the Thread?
| >>
| >>
| >>
| >
| >
|
|
|
 
A

AdrianJMartin

Hi Steven,

Why would it be an OS thread I see? VS debugging is set to Managed only, and
I've name the thread. if you run the code below, press the button a few
times, snap on a debugger break point inside Button1_Click(...) and see that
thread window is full of orphaned threads.( you may have Comment out the
t.Join )


Adrian






Steven Cheng said:
Hi Adrian,

If the hang or unreleased thread you watched is the underlying operating
system thread, then that should be reasonable since regardless of the
thread operation (start, end,.....), the CLR's unmanaged code has
structure
to hold reference to operating system thread. And so there is no definite
mapping between the underlying operating system thread to the managed CLR
thread instance..... Sometimes, our CLR t hread's work has finished, the
underlying CLR structure may still hold the OS thread. Or other times
event
the OS thread has destroyed, the CLR's thread reference still refer to a
managed thread object (for keepting some thread information....)...

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "AdrianJMartin" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Threading
| Date: Mon, 19 Dec 2005 09:43:27 -0000
| Lines: 77
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:365658
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I DO need an STA thread, I'm creating an invisible window that hosts
mshtml
| and axwebrowser. I'm dynamically creating a bitmap using html. I need
some
| consistancy across platforms, I have this code working fine. But I have
to
| create a thread(STA) to host it.
|
| In my testing i've noticed that the thread hangs arround in the
debugger.
| Even stripping my component out and creating the do almost nothing
thread
| example that i posted leaves this 'orphaned' thread. Regardless of the
| apartment state.
|
| I can't use the threadpool as the those threads are always MTA.
|
|
| "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc> wrote in message
| | >I see no reason for STA thread based on the example you provided. Care
to
| > elaborate? STA threads are for COM based process by the way.
| >
| > sorry, scratch that. I realized you were simply using incorrect
| > terminology.
| >
| > --
| > Regards,
| > Alvin Bruney [MVP ASP.NET]
| >
| > [Shameless Author plug]
| > The Microsoft Office Web Components Black Book with .NET
| > Now Available @ www.lulu.com/owc
| > Forth-coming VSTO.NET - Wrox/Wiley 2006
| > -------------------------------------------------------
| >
| >
| >
| > | >> Hi all,
| >>
| >> I have a need for a STA thread from asp.net. I can create the thread
and
| > it
| >> runs fine. But when it is finished, the thread still 'hangs' arround.
| >> Visible only to the debugger.....
| >> I get the "The thread has exited with code 0 (0x12fc)." in the
debugger
| >> each time the thread seemly end, but its still there in the thread
| >> window.
| >> GC.Collect() does not get rid of them either, something must be
holding a
| >> reference to the threads.........
| >>
| >>
| >> a trivial example that demonstrates this
| >>
| >> public void Work()
| >> {
| >> Thread.Sleep( 5000 ) ;
| >> System.Diagnostics.Debug.WriteLine("Done!");
| >> }
| >>
| >> private void Button1_Click(object sender, System.EventArgs e)
| >> {
| >> ThreadStart ts = new ThreadStart( Work ) ;
| >> Thread t = new Thread( ts );
| >> t.Name = "test";
| >> t.Start();
| >> t.Join();
| >> Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
| >> t = null ;
| >> GC.Collect();
| >> }
| >>
| >>
| >> Is there anyway to clean up the Thread?
| >>
| >>
| >>
| >
| >
|
|
|
 
S

Steven Cheng[MSFT]

Hi Adrian,

Thanks for your quick response.
For the Process.GetCurrentProcess().Threads, it's a collection that
represents all the OS threads in the current running process(retrieve from
server's performance counter), we can see that the Thread's class is
"ProcessThread" rather than the System.Threading.Thread.....

Also, based on my local test, when creating a new thread that in STA
apartment model, it'll end successfully (the OS thread will also be
destoryed....). And for the .net's managed Thread class, it's threadState
will be set to "Stoped" when it has finished the work, we no longer need to
worry about the sequential work since the CLR will take care of it.....

Here is a test page I used to print the process threads and the managed
threads (I manually created)'s status......

I contains three buttons , one used to printout OS threads, one used to
create new STA threads, and the third print out my managed threads:

=============================
public class ThreadTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnPrintThreads;
protected System.Web.UI.WebControls.Button btnStartThreads;
protected System.Web.UI.WebControls.Button btnPrintManagedThreads;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPrintThreads.Click += new
System.EventHandler(this.btnPrintThreads_Click);
this.btnStartThreads.Click += new
System.EventHandler(this.btnStartThreads_Click);
this.btnPrintManagedThreads.Click += new
System.EventHandler(this.btnPrintManagedThreads_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnStartThreads_Click(object sender, System.EventArgs e)
{
Thread td = new Thread(new ThreadStart(Work));



if(Application["threads"] == null)
{
Application["threads"] = new ArrayList();
}

ArrayList list = Application["threads"] as ArrayList;

list.Add(td);

td.ApartmentState = ApartmentState.STA;
td.Start();

}

private void btnPrintThreads_Click(object sender, System.EventArgs e)
{
Response.Write("<br>==================");
Response.Write("<br>OS threads: " +
Process.GetCurrentProcess().Threads.Count);

foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
Response.Write("<br>Id: " + pt.Id + " Status: " + pt.ThreadState);
}
}

private void btnPrintManagedThreads_Click(object sender, System.EventArgs
e)
{
if(Application["threads"] == null)
{
Application["threads"] = new ArrayList();
}

ArrayList list = Application["threads"] as ArrayList;

Response.Write("<br>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
Response.Write("<br>Worker Threads: " + list.Count);

foreach(object obj in list)
{
Thread td = obj as Thread;
if(td != null)
{
Response.Write("<br>Id: " + td.Name + " status: " + td.ThreadState);
}
}


}


public static void Work()
{
Thread.Sleep( 10000 ) ;
System.Diagnostics.Debug.WriteLine("Done!");
}
}
============================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "AdrianJMartin" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Threading
| Date: Tue, 20 Dec 2005 09:49:04 -0000
| Lines: 153
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:365906
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi Steven,
|
| Why would it be an OS thread I see? VS debugging is set to Managed only,
and
| I've name the thread. if you run the code below, press the button a few
| times, snap on a debugger break point inside Button1_Click(...) and see
that
| thread window is full of orphaned threads.( you may have Comment out the
| t.Join )
|
|
| Adrian
|
|
|
|
|
|
| | > Hi Adrian,
| >
| > If the hang or unreleased thread you watched is the underlying operating
| > system thread, then that should be reasonable since regardless of the
| > thread operation (start, end,.....), the CLR's unmanaged code has
| > structure
| > to hold reference to operating system thread. And so there is no
definite
| > mapping between the underlying operating system thread to the managed
CLR
| > thread instance..... Sometimes, our CLR t hread's work has finished, the
| > underlying CLR structure may still hold the OS thread. Or other times
| > event
| > the OS thread has destroyed, the CLR's thread reference still refer to a
| > managed thread object (for keepting some thread information....)...
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | From: "AdrianJMartin" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > | Subject: Re: Threading
| > | Date: Mon, 19 Dec 2005 09:43:27 -0000
| > | Lines: 77
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:365658
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | I DO need an STA thread, I'm creating an invisible window that hosts
| > mshtml
| > | and axwebrowser. I'm dynamically creating a bitmap using html. I need
| > some
| > | consistancy across platforms, I have this code working fine. But I
have
| > to
| > | create a thread(STA) to host it.
| > |
| > | In my testing i've noticed that the thread hangs arround in the
| > debugger.
| > | Even stripping my component out and creating the do almost nothing
| > thread
| > | example that i posted leaves this 'orphaned' thread. Regardless of the
| > | apartment state.
| > |
| > | I can't use the threadpool as the those threads are always MTA.
| > |
| > |
| > | "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc> wrote in message
| > | | > | >I see no reason for STA thread based on the example you provided.
Care
| > to
| > | > elaborate? STA threads are for COM based process by the way.
| > | >
| > | > sorry, scratch that. I realized you were simply using incorrect
| > | > terminology.
| > | >
| > | > --
| > | > Regards,
| > | > Alvin Bruney [MVP ASP.NET]
| > | >
| > | > [Shameless Author plug]
| > | > The Microsoft Office Web Components Black Book with .NET
| > | > Now Available @ www.lulu.com/owc
| > | > Forth-coming VSTO.NET - Wrox/Wiley 2006
| > | > -------------------------------------------------------
| > | >
| > | >
| > | >
| > | > | > | >> Hi all,
| > | >>
| > | >> I have a need for a STA thread from asp.net. I can create the
thread
| > and
| > | > it
| > | >> runs fine. But when it is finished, the thread still 'hangs'
arround.
| > | >> Visible only to the debugger.....
| > | >> I get the "The thread has exited with code 0 (0x12fc)." in the
| > debugger
| > | >> each time the thread seemly end, but its still there in the thread
| > | >> window.
| > | >> GC.Collect() does not get rid of them either, something must be
| > holding a
| > | >> reference to the threads.........
| > | >>
| > | >>
| > | >> a trivial example that demonstrates this
| > | >>
| > | >> public void Work()
| > | >> {
| > | >> Thread.Sleep( 5000 ) ;
| > | >> System.Diagnostics.Debug.WriteLine("Done!");
| > | >> }
| > | >>
| > | >> private void Button1_Click(object sender, System.EventArgs e)
| > | >> {
| > | >> ThreadStart ts = new ThreadStart( Work ) ;
| > | >> Thread t = new Thread( ts );
| > | >> t.Name = "test";
| > | >> t.Start();
| > | >> t.Join();
| > | >> Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
| > | >> t = null ;
| > | >> GC.Collect();
| > | >> }
| > | >>
| > | >>
| > | >> Is there anyway to clean up the Thread?
| > | >>
| > | >>
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 
A

AdrianJMartin

Thank you Steven you have been very helpfull!

I modded your code to generate lots of threads per click, initialy they seem
to hang arround in the debugger. but i added some more code to remove
stopped threads from the arraylist( i assume they were still reachable
because of the reference in the arraylist). And now they are gone! GOOD.

I went back to my simpler code and noticed that if i detach and reattach the
debugger to aspnet_wp.exe then the thread window get updated correctly, so
i've been lead a bit of a dance by the vs2003 debugger!



Ta
Adrian






Steven Cheng said:
Hi Adrian,

Thanks for your quick response.
For the Process.GetCurrentProcess().Threads, it's a collection that
represents all the OS threads in the current running process(retrieve from
server's performance counter), we can see that the Thread's class is
"ProcessThread" rather than the System.Threading.Thread.....

Also, based on my local test, when creating a new thread that in STA
apartment model, it'll end successfully (the OS thread will also be
destoryed....). And for the .net's managed Thread class, it's threadState
will be set to "Stoped" when it has finished the work, we no longer need
to
worry about the sequential work since the CLR will take care of it.....

Here is a test page I used to print the process threads and the managed
threads (I manually created)'s status......

I contains three buttons , one used to printout OS threads, one used to
create new STA threads, and the third print out my managed threads:

=============================
public class ThreadTest : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnPrintThreads;
protected System.Web.UI.WebControls.Button btnStartThreads;
protected System.Web.UI.WebControls.Button btnPrintManagedThreads;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPrintThreads.Click += new
System.EventHandler(this.btnPrintThreads_Click);
this.btnStartThreads.Click += new
System.EventHandler(this.btnStartThreads_Click);
this.btnPrintManagedThreads.Click += new
System.EventHandler(this.btnPrintManagedThreads_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnStartThreads_Click(object sender, System.EventArgs e)
{
Thread td = new Thread(new ThreadStart(Work));



if(Application["threads"] == null)
{
Application["threads"] = new ArrayList();
}

ArrayList list = Application["threads"] as ArrayList;

list.Add(td);

td.ApartmentState = ApartmentState.STA;
td.Start();

}

private void btnPrintThreads_Click(object sender, System.EventArgs e)
{
Response.Write("<br>==================");
Response.Write("<br>OS threads: " +
Process.GetCurrentProcess().Threads.Count);

foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
{
Response.Write("<br>Id: " + pt.Id + " Status: " + pt.ThreadState);
}
}

private void btnPrintManagedThreads_Click(object sender, System.EventArgs
e)
{
if(Application["threads"] == null)
{
Application["threads"] = new ArrayList();
}

ArrayList list = Application["threads"] as ArrayList;

Response.Write("<br>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
Response.Write("<br>Worker Threads: " + list.Count);

foreach(object obj in list)
{
Thread td = obj as Thread;
if(td != null)
{
Response.Write("<br>Id: " + td.Name + " status: " + td.ThreadState);
}
}


}


public static void Work()
{
Thread.Sleep( 10000 ) ;
System.Diagnostics.Debug.WriteLine("Done!");
}
}
============================

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "AdrianJMartin" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Threading
| Date: Tue, 20 Dec 2005 09:49:04 -0000
| Lines: 153
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:365906
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hi Steven,
|
| Why would it be an OS thread I see? VS debugging is set to Managed only,
and
| I've name the thread. if you run the code below, press the button a few
| times, snap on a debugger break point inside Button1_Click(...) and see
that
| thread window is full of orphaned threads.( you may have Comment out the
| t.Join )
|
|
| Adrian
|
|
|
|
|
|
| | > Hi Adrian,
| >
| > If the hang or unreleased thread you watched is the underlying
operating
| > system thread, then that should be reasonable since regardless of the
| > thread operation (start, end,.....), the CLR's unmanaged code has
| > structure
| > to hold reference to operating system thread. And so there is no
definite
| > mapping between the underlying operating system thread to the managed
CLR
| > thread instance..... Sometimes, our CLR t hread's work has finished,
the
| > underlying CLR structure may still hold the OS thread. Or other times
| > event
| > the OS thread has destroyed, the CLR's thread reference still refer to
a
| > managed thread object (for keepting some thread information....)...
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | From: "AdrianJMartin" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > | Subject: Re: Threading
| > | Date: Mon, 19 Dec 2005 09:43:27 -0000
| > | Lines: 77
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| > | Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:365658
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | I DO need an STA thread, I'm creating an invisible window that hosts
| > mshtml
| > | and axwebrowser. I'm dynamically creating a bitmap using html. I
need
| > some
| > | consistancy across platforms, I have this code working fine. But I
have
| > to
| > | create a thread(STA) to host it.
| > |
| > | In my testing i've noticed that the thread hangs arround in the
| > debugger.
| > | Even stripping my component out and creating the do almost nothing
| > thread
| > | example that i posted leaves this 'orphaned' thread. Regardless of
the
| > | apartment state.
| > |
| > | I can't use the threadpool as the those threads are always MTA.
| > |
| > |
| > | "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc> wrote in message
| > | | > | >I see no reason for STA thread based on the example you provided.
Care
| > to
| > | > elaborate? STA threads are for COM based process by the way.
| > | >
| > | > sorry, scratch that. I realized you were simply using incorrect
| > | > terminology.
| > | >
| > | > --
| > | > Regards,
| > | > Alvin Bruney [MVP ASP.NET]
| > | >
| > | > [Shameless Author plug]
| > | > The Microsoft Office Web Components Black Book with .NET
| > | > Now Available @ www.lulu.com/owc
| > | > Forth-coming VSTO.NET - Wrox/Wiley 2006
| > | > -------------------------------------------------------
| > | >
| > | >
| > | >
| > | > | > | >> Hi all,
| > | >>
| > | >> I have a need for a STA thread from asp.net. I can create the
thread
| > and
| > | > it
| > | >> runs fine. But when it is finished, the thread still 'hangs'
arround.
| > | >> Visible only to the debugger.....
| > | >> I get the "The thread has exited with code 0 (0x12fc)." in the
| > debugger
| > | >> each time the thread seemly end, but its still there in the
thread
| > | >> window.
| > | >> GC.Collect() does not get rid of them either, something must be
| > holding a
| > | >> reference to the threads.........
| > | >>
| > | >>
| > | >> a trivial example that demonstrates this
| > | >>
| > | >> public void Work()
| > | >> {
| > | >> Thread.Sleep( 5000 ) ;
| > | >> System.Diagnostics.Debug.WriteLine("Done!");
| > | >> }
| > | >>
| > | >> private void Button1_Click(object sender, System.EventArgs e)
| > | >> {
| > | >> ThreadStart ts = new ThreadStart( Work ) ;
| > | >> Thread t = new Thread( ts );
| > | >> t.Name = "test";
| > | >> t.Start();
| > | >> t.Join();
| > | >> Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
| > | >> t = null ;
| > | >> GC.Collect();
| > | >> }
| > | >>
| > | >>
| > | >> Is there anyway to clean up the Thread?
| > | >>
| > | >>
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|
 
S

Steven Cheng[MSFT]

Thanks for your followup Adrian,

Yes, that's also why I'd prefered test through the page out of debugger (or
in debuggin mode...) :)...

Have a good day!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
| From: "AdrianJMartin" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: Threading
| Date: Wed, 21 Dec 2005 09:59:55 -0000
| Lines: 352
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:366188
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Thank you Steven you have been very helpfull!
|
| I modded your code to generate lots of threads per click, initialy they
seem
| to hang arround in the debugger. but i added some more code to remove
| stopped threads from the arraylist( i assume they were still reachable
| because of the reference in the arraylist). And now they are gone! GOOD.
|
| I went back to my simpler code and noticed that if i detach and reattach
the
| debugger to aspnet_wp.exe then the thread window get updated correctly,
so
| i've been lead a bit of a dance by the vs2003 debugger!
|
|
|
| Ta
| Adrian
|
|
|
|
|
|
| | > Hi Adrian,
| >
| > Thanks for your quick response.
| > For the Process.GetCurrentProcess().Threads, it's a collection that
| > represents all the OS threads in the current running process(retrieve
from
| > server's performance counter), we can see that the Thread's class is
| > "ProcessThread" rather than the System.Threading.Thread.....
| >
| > Also, based on my local test, when creating a new thread that in STA
| > apartment model, it'll end successfully (the OS thread will also be
| > destoryed....). And for the .net's managed Thread class, it's
threadState
| > will be set to "Stoped" when it has finished the work, we no longer
need
| > to
| > worry about the sequential work since the CLR will take care of it.....
| >
| > Here is a test page I used to print the process threads and the managed
| > threads (I manually created)'s status......
| >
| > I contains three buttons , one used to printout OS threads, one used to
| > create new STA threads, and the third print out my managed threads:
| >
| > =============================
| > public class ThreadTest : System.Web.UI.Page
| > {
| > protected System.Web.UI.WebControls.Button btnPrintThreads;
| > protected System.Web.UI.WebControls.Button btnStartThreads;
| > protected System.Web.UI.WebControls.Button btnPrintManagedThreads;
| >
| > private void Page_Load(object sender, System.EventArgs e)
| > {
| > // Put user code to initialize the page here
| > }
| >
| > #region Web Form Designer generated code
| > override protected void OnInit(EventArgs e)
| > {
| > //
| > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
| > //
| > InitializeComponent();
| > base.OnInit(e);
| > }
| >
| > /// <summary>
| > /// Required method for Designer support - do not modify
| > /// the contents of this method with the code editor.
| > /// </summary>
| > private void InitializeComponent()
| > {
| > this.btnPrintThreads.Click += new
| > System.EventHandler(this.btnPrintThreads_Click);
| > this.btnStartThreads.Click += new
| > System.EventHandler(this.btnStartThreads_Click);
| > this.btnPrintManagedThreads.Click += new
| > System.EventHandler(this.btnPrintManagedThreads_Click);
| > this.Load += new System.EventHandler(this.Page_Load);
| >
| > }
| > #endregion
| >
| > private void btnStartThreads_Click(object sender, System.EventArgs e)
| > {
| > Thread td = new Thread(new ThreadStart(Work));
| >
| >
| >
| > if(Application["threads"] == null)
| > {
| > Application["threads"] = new ArrayList();
| > }
| >
| > ArrayList list = Application["threads"] as ArrayList;
| >
| > list.Add(td);
| >
| > td.ApartmentState = ApartmentState.STA;
| > td.Start();
| >
| > }
| >
| > private void btnPrintThreads_Click(object sender, System.EventArgs e)
| > {
| > Response.Write("<br>==================");
| > Response.Write("<br>OS threads: " +
| > Process.GetCurrentProcess().Threads.Count);
| >
| > foreach(ProcessThread pt in Process.GetCurrentProcess().Threads)
| > {
| > Response.Write("<br>Id: " + pt.Id + " Status: " + pt.ThreadState);
| > }
| > }
| >
| > private void btnPrintManagedThreads_Click(object sender,
System.EventArgs
| > e)
| > {
| > if(Application["threads"] == null)
| > {
| > Application["threads"] = new ArrayList();
| > }
| >
| > ArrayList list = Application["threads"] as ArrayList;
| >
| > Response.Write("<br>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
| > Response.Write("<br>Worker Threads: " + list.Count);
| >
| > foreach(object obj in list)
| > {
| > Thread td = obj as Thread;
| > if(td != null)
| > {
| > Response.Write("<br>Id: " + td.Name + " status: " + td.ThreadState);
| > }
| > }
| >
| >
| > }
| >
| >
| > public static void Work()
| > {
| > Thread.Sleep( 10000 ) ;
| > System.Diagnostics.Debug.WriteLine("Done!");
| > }
| > }
| > ============================
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| > --------------------
| > | From: "AdrianJMartin" <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > <[email protected]>
| > | Subject: Re: Threading
| > | Date: Tue, 20 Dec 2005 09:49:04 -0000
| > | Lines: 153
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| > | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| > | Xref: TK2MSFTNGXA02.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:365906
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Hi Steven,
| > |
| > | Why would it be an OS thread I see? VS debugging is set to Managed
only,
| > and
| > | I've name the thread. if you run the code below, press the button a
few
| > | times, snap on a debugger break point inside Button1_Click(...) and
see
| > that
| > | thread window is full of orphaned threads.( you may have Comment out
the
| > | t.Join )
| > |
| > |
| > | Adrian
| > |
| > |
| > |
| > |
| > |
| > |
| > | | > | > Hi Adrian,
| > | >
| > | > If the hang or unreleased thread you watched is the underlying
| > operating
| > | > system thread, then that should be reasonable since regardless of
the
| > | > thread operation (start, end,.....), the CLR's unmanaged code has
| > | > structure
| > | > to hold reference to operating system thread. And so there is no
| > definite
| > | > mapping between the underlying operating system thread to the
managed
| > CLR
| > | > thread instance..... Sometimes, our CLR t hread's work has
finished,
| > the
| > | > underlying CLR structure may still hold the OS thread. Or other
times
| > | > event
| > | > the OS thread has destroyed, the CLR's thread reference still refer
to
| > a
| > | > managed thread object (for keepting some thread information....)...
| > | >
| > | > Thanks,
| > | >
| > | > Steven Cheng
| > | > Microsoft Online Support
| > | >
| > | > Get Secure! www.microsoft.com/security
| > | > (This posting is provided "AS IS", with no warranties, and confers
no
| > | > rights.)
| > | >
| > | >
| > | >
| > | > --------------------
| > | > | From: "AdrianJMartin" <[email protected]>
| > | > | References: <[email protected]>
| > | > <[email protected]>
| > | > | Subject: Re: Threading
| > | > | Date: Mon, 19 Dec 2005 09:43:27 -0000
| > | > | Lines: 77
| > | > | X-Priority: 3
| > | > | X-MSMail-Priority: Normal
| > | > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | > | X-RFC2646: Format=Flowed; Original
| > | > | Message-ID: <[email protected]>
| > | > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | > | NNTP-Posting-Host: ip-217-204-53-234.easynet.co.uk 217.204.53.234
| > | > | Path:
| > TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| > | > | Xref: TK2MSFTNGXA02.phx.gbl
| > | > microsoft.public.dotnet.framework.aspnet:365658
| > | > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > | > |
| > | > | I DO need an STA thread, I'm creating an invisible window that
hosts
| > | > mshtml
| > | > | and axwebrowser. I'm dynamically creating a bitmap using html. I
| > need
| > | > some
| > | > | consistancy across platforms, I have this code working fine. But I
| > have
| > | > to
| > | > | create a thread(STA) to host it.
| > | > |
| > | > | In my testing i've noticed that the thread hangs arround in the
| > | > debugger.
| > | > | Even stripping my component out and creating the do almost nothing
| > | > thread
| > | > | example that i posted leaves this 'orphaned' thread. Regardless
of
| > the
| > | > | apartment state.
| > | > |
| > | > | I can't use the threadpool as the those threads are always MTA.
| > | > |
| > | > |
| > | > | "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc> wrote in message
| > | > | | > | > | >I see no reason for STA thread based on the example you provided.
| > Care
| > | > to
| > | > | > elaborate? STA threads are for COM based process by the way.
| > | > | >
| > | > | > sorry, scratch that. I realized you were simply using incorrect
| > | > | > terminology.
| > | > | >
| > | > | > --
| > | > | > Regards,
| > | > | > Alvin Bruney [MVP ASP.NET]
| > | > | >
| > | > | > [Shameless Author plug]
| > | > | > The Microsoft Office Web Components Black Book with .NET
| > | > | > Now Available @ www.lulu.com/owc
| > | > | > Forth-coming VSTO.NET - Wrox/Wiley 2006
| > | > | > -------------------------------------------------------
| > | > | >
| > | > | >
| > | > | >
message
| > | > | > | > | > | >> Hi all,
| > | > | >>
| > | > | >> I have a need for a STA thread from asp.net. I can create the
| > thread
| > | > and
| > | > | > it
| > | > | >> runs fine. But when it is finished, the thread still 'hangs'
| > arround.
| > | > | >> Visible only to the debugger.....
| > | > | >> I get the "The thread has exited with code 0 (0x12fc)." in the
| > | > debugger
| > | > | >> each time the thread seemly end, but its still there in the
| > thread
| > | > | >> window.
| > | > | >> GC.Collect() does not get rid of them either, something must be
| > | > holding a
| > | > | >> reference to the threads.........
| > | > | >>
| > | > | >>
| > | > | >> a trivial example that demonstrates this
| > | > | >>
| > | > | >> public void Work()
| > | > | >> {
| > | > | >> Thread.Sleep( 5000 ) ;
| > | > | >> System.Diagnostics.Debug.WriteLine("Done!");
| > | > | >> }
| > | > | >>
| > | > | >> private void Button1_Click(object sender, System.EventArgs e)
| > | > | >> {
| > | > | >> ThreadStart ts = new ThreadStart( Work ) ;
| > | > | >> Thread t = new Thread( ts );
| > | > | >> t.Name = "test";
| > | > | >> t.Start();
| > | > | >> t.Join();
| > | > | >> Debug.WriteLine( Process.GetCurrentProcess().Threads.Count );
| > | > | >> t = null ;
| > | > | >> GC.Collect();
| > | > | >> }
| > | > | >>
| > | > | >>
| > | > | >> Is there anyway to clean up the Thread?
| > | > | >>
| > | > | >>
| > | > | >>
| > | > | >
| > | > | >
| > | > |
| > | > |
| > | > |
| > | >
| > |
| > |
| > |
| >
|
|
|
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top