I wrote some code in a method called in a separate thread via the ThreadPool
where I expected my code in a Finally section to be called but it never did.
I'd be interested to know why this is the case? Here's some code to repro
the behaviour:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
class Program
{
static void DoSomething( object state )
{
try
{
int count = 0;
while( true )
{
Console.WriteLine( count++ );
System.Threading.Thread.Sleep( 500 );
}
}
finally
{
Console.WriteLine( "Thread Finished" );
}
}
static void Main( string[] args )
{
System.Threading.ThreadPool.QueueUserWorkItem( new
System.Threading.WaitCallback( DoSomething ) );
System.Threading.Thread.Sleep( 5000 );
System.Console.WriteLine( "Process Done" );
}
}
}
Regards
Lee Alexander www.feedghost.com
An RSS Reader for Vista & XP
Synchronize, search, tag and share. 8 1813
while(true) will never exit...
even if you were hoping for an int overflow (that won't happen) it
would take, what, 34 years?
Marc
For completeness - your app is exiting (without calling the finally)
because the pool thread is a *background* thread, and therefore
doesn't keep the exe alive. When the last foreground thread exits, the
process dies. Finally does not get respected if the process is ripped
down.
Marc
On Jun 28, 2:14 pm, "Marc Gravell" <marc.grav...@gmail.comwrote:
while(true) will never exit...
even if you were hoping for an int overflow (that won't happen) it
would take, what, 34 years?
My guess is that the OP was hoping that the application exiting would
effectively trigger a Thread.Abort in all background threads, which
would indeed cause the finally block to execute. Looks like this isn't
the case (or at least isn't always the case), despite what http://www.devsource.com/article2/0,1895,1952643,00.asp says.
Jon
"Jon Skeet [C# MVP]" <sk***@pobox.comschrieb im Newsbeitrag
news:11**********************@n2g2000hse.googlegro ups.com...
On Jun 28, 2:14 pm, "Marc Gravell" <marc.grav...@gmail.comwrote:
>while(true) will never exit...
even if you were hoping for an int overflow (that won't happen) it would take, what, 34 years?
My guess is that the OP was hoping that the application exiting would
effectively trigger a Thread.Abort in all background threads, which
would indeed cause the finally block to execute. Looks like this isn't
the case (or at least isn't always the case), despite what http://www.devsource.com/article2/0,1895,1952643,00.asp says.
Jon
From this link: http://msdn2.microsoft.com/en-us/lib...d0(vs.80).aspx
shutting down does not throw an AbortException in backgrpund threads.
Christof
Marc,
This wasn't the issue (just example code), I was just trying to show that
finally never got called. Sorry if I didn't explain myself properly...
Anyway thanks for replying
Regards
Lee
"Marc Gravell" <ma**********@gmail.comwrote in message
news:On**************@TK2MSFTNGP04.phx.gbl...
while(true) will never exit...
even if you were hoping for an int overflow (that won't happen) it would
take, what, 34 years?
Marc
On Jun 29, 2:44 pm, Peter Ritchie [C# MVP] <PRS...@newsgroups.nospam>
wrote:
<snip>
Another issue with terminating a thread either explicitly or implicitly is
if that thread is in the constructor for an object. In that case you can no
longer create objects of that type because the c'tor locks the class, if it
doesn't exit properly it won't unlock the class. This is the primary reason
why Thread.Terminate and Thread.Pause were made obsolete.
Gosh, I wasn't aware of that. Any idea *why* it does that? Also, if
that's the case, why does the following code appear to be running
multiple threads within a constructor?
using System;
using System.Threading;
class Test
{
public Test(int number)
{
for (int i=0; i < 1000; i++)
{
Console.WriteLine ("Constructor {0}: {1}", number, i);
Thread.Sleep(500);
}
}
static void Main()
{
for (int i=0; i < 5; i++)
{
new Thread (Count).Start(i);
}
}
static void Count(object state)
{
new Test((int)state);
}
}
I don't suppose you meant the *static* constructor, did you? That
would certainly make sense (and be much less of a risk).
Jon
Actually yes, that should be static constructors and synchronized methods.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote. http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jon Skeet [C# MVP]" wrote:
On Jun 29, 2:44 pm, Peter Ritchie [C# MVP] <PRS...@newsgroups.nospam>
wrote:
<snip>
Another issue with terminating a thread either explicitly or implicitly is
if that thread is in the constructor for an object. In that case you can no
longer create objects of that type because the c'tor locks the class, if it
doesn't exit properly it won't unlock the class. This is the primary reason
why Thread.Terminate and Thread.Pause were made obsolete.
Gosh, I wasn't aware of that. Any idea *why* it does that? Also, if
that's the case, why does the following code appear to be running
multiple threads within a constructor?
using System;
using System.Threading;
class Test
{
public Test(int number)
{
for (int i=0; i < 1000; i++)
{
Console.WriteLine ("Constructor {0}: {1}", number, i);
Thread.Sleep(500);
}
}
static void Main()
{
for (int i=0; i < 5; i++)
{
new Thread (Count).Start(i);
}
}
static void Count(object state)
{
new Test((int)state);
}
}
I don't suppose you meant the *static* constructor, did you? That
would certainly make sense (and be much less of a risk).
Jon
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: djw |
last post by:
Hi, Folks-
I have a question regarding the "proper" use of try: finally:... Consider
some code like this:
d = Device.open()
try:
d.someMethodThatCanRaiseError(...)
if SomeCondition:
raise...
|
by: cottonviking |
last post by:
Greetings, all! I've been pondering the pitfalls of aborting a
secondary thread in a service app I'm writing (VB, fx v1.1).
Everything I've read so far pretty much dissuades one from aborting one...
|
by: Bill |
last post by:
Say I have a childThread currently is running a finally block to
cleanup external resources. At the same time the main thread calls
childThread.Abort(). The question is: when the...
|
by: Chris |
last post by:
Hi,
regarding exception-handling :
why put code in a 'finally' block, and not just after a 'catch'-block -->
Example using 'finally ' :
try
{
OpenFile();
|
by: Andy Turner |
last post by:
I was just pondering about what the Finally in an exception actually
does. Well, I realise that it always gets called regardless of whether
the exception is thrown or not, but isn't that the same...
|
by: Jon Davis |
last post by:
I understand that the finally sub-block will execute regardless of whether
try succeeded or not. But so will code that follows try...catch. So then
what is the difference between ...
try {...
|
by: Eran.Yasso |
last post by:
Hi,
I wondering where should i use finally and where should i use catch?
I have a thread, which the thread dispatcher waits for the thread. if i
abort the
thread, an exception happens. why...
|
by: George Sakkis |
last post by:
I posted this on the Pyro list but I'm not sure if it's related
specifically to Pyro. The "finally" clause below is not executed when
f() runs on on a (daemon) thread and the program exits. DAEMON...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |