473,382 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

finally vs catch in thread abort

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 should i use finally and not catch as
below?

for exmple?

private void button1_Click(object sender, EventArgs e)
{
myThread = new Thread(myThreadDelegate);
myThread.Start();
}

void threadcreatore()
{
try
{
Workerthread Workerthreadob = new Workerthread();
Workerthreadob.thdRunnerEngine();
}
finally
{
MessageBox.Show("In finally");
}
}

private void button2_Click(object sender, EventArgs e)
{
myThread.Abort();
}

You can see good sample in
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

thanks,

Dec 25 '06 #1
3 2473
Using Abort to stop a thread is the worst possible, "brute force" way to do
this.
Mr Skeet's examples on threading are most elegant and quite "best
practices". Suggest you read more of his material.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Er********@gmail.com" wrote:
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 should i use finally and not catch as
below?

for exmple?

private void button1_Click(object sender, EventArgs e)
{
myThread = new Thread(myThreadDelegate);
myThread.Start();
}

void threadcreatore()
{
try
{
Workerthread Workerthreadob = new Workerthread();
Workerthreadob.thdRunnerEngine();
}
finally
{
MessageBox.Show("In finally");
}
}

private void button2_Click(object sender, EventArgs e)
{
myThread.Abort();
}

You can see good sample in
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

thanks,

Dec 25 '06 #2

Peter wrote:
Using Abort to stop a thread is the worst possible, "brute force" way to do
this.
Mr Skeet's examples on threading are most elegant and quite "best
practices". Suggest you read more of his material.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Er********@gmail.com" wrote:
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 should i use finally and not catch as
below?

for exmple?

private void button1_Click(object sender, EventArgs e)
{
myThread = new Thread(myThreadDelegate);
myThread.Start();
}

void threadcreatore()
{
try
{
Workerthread Workerthreadob = new Workerthread();
Workerthreadob.thdRunnerEngine();
}
finally
{
MessageBox.Show("In finally");
}
}

private void button2_Click(object sender, EventArgs e)
{
myThread.Abort();
}

You can see good sample in
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

thanks,
I am using the abort method because my thread is process.start() on
wait. the only way
to kill the thread while is systemWait command is the abort.

Dec 25 '06 #3
Rule of thumb, finally is always fired whether there is exception or no
exception.

This is a good place to do clean up. Even if you have something like
return inside try catch finally is executed.


Er********@gmail.com wrote:
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 should i use finally and not catch as
below?

for exmple?

private void button1_Click(object sender, EventArgs e)
{
myThread = new Thread(myThreadDelegate);
myThread.Start();
}

void threadcreatore()
{
try
{
Workerthread Workerthreadob = new Workerthread();
Workerthreadob.thdRunnerEngine();
}
finally
{
MessageBox.Show("In finally");
}
}

private void button2_Click(object sender, EventArgs e)
{
myThread.Abort();
}

You can see good sample in
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

thanks,
Dec 26 '06 #4

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

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
2
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...
16
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...
16
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();
13
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 {...
2
by: Wayne | last post by:
I have code that Posts Form values to another page. I am using a Try Catch Finally to handle thrown exceptions from our core logic. The problem is if everything is successful it Posts the...
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
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...
0
isladogs
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...

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.