Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > .NET Framework 2.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
.NET Framework 2.0 For discussion of the Microsoft .NET Framework 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 2.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 3rd, 2008, 08:01 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default Problem with Thread.abort

Hi All,

 I have problem in aborting the thread. Below is the code for Creating thread.

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 Dim operation as new ThreadStart(simplework)

 Dim objLoadThread As New System.Threading.Thread(operation)

 objLoadThread.Start()

 End sub

 Private Sub simplework()

 // some code

 End sub


 I am trying to Abort the thread in Button click event like following code.

 Protected Sub btnAbort_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAbort.Click

            objLoadThread.Abort()

 End Sub


 Do i need to kill the thread in Pageload method only. Can i abort the thread in button click ?

 please tell me where i m doing wrong.


 Thanks in advance.


 -Nelly
Reply With Quote
  #2 (permalink)  
Old April 3rd, 2008, 08:12 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

a) You are creating a local variable inside the Page_Load method. This variable will not be available inside other methods, like btnAbort_Click1. Therefore your code wont even compile.

b) You are using ASP.Net - Page_Load is run everytime the page loads - this includes the first time the page is viewed, and again when the button is clicked, so you just started at least 2 new threads. Which one do you expect the abort button to try and abort?

c) HTTP is stateless - your page is created, destroyed and recreated every time you refresh the page. Even if you stored the thread as a page property it wouldn't exist (or at least not point to the same Thread) once the page was reloaded.



/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old April 3rd, 2008, 08:24 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi thanks for your reply.

It was mistake , i m delaring thread above the page load method...

like the following code:

Dim operation as new ThreadStart(simplework)

Dim objLoadThread As New System.Threading.Thread(operation)


 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  objLoadThread.Start()

 End sub

 Private Sub simplework()

 // some code

 End sub


 I am trying to Abort the thread in Button click event like following code.

 Protected Sub btnAbort_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAbort.Click

            objLoadThread.Abort()

 End Sub


Regads,
-nelly
Reply With Quote
  #4 (permalink)  
Old April 3rd, 2008, 08:27 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

My mistake. I assumed you would read all of my last post.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old April 3rd, 2008, 08:36 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Sam,

Thanks for your replies.

If i use If Not IsPostBack in page means like:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not IsPostBack Then

          objLoadThread.Start()

        End if
End sub

Now it will Start only once, m i right??

Please shed some light.

Thanks
nelly
Reply With Quote
  #6 (permalink)  
Old April 3rd, 2008, 08:40 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

Correct. Now when you click the abort button objLoadThread will be null.

See point 3.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #7 (permalink)  
Old April 3rd, 2008, 08:50 AM
Authorized User
Points: 437, Level: 7
Points: 437, Level: 7 Points: 437, Level: 7 Points: 437, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , , .
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Correct.
can u please tell.
How to kill that thread which have i started ?

because after killing the thread i have to do some database work like deleting the table which i have created after stating the thread.

can i do that ?

-nelly
Reply With Quote
  #8 (permalink)  
Old April 3rd, 2008, 09:10 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.. Maybe I'm confused, but unless that thread die by itself, you will end up with a lot of new threads that are not killing by anyone..

If you are in a web enviroment, you could always close your explorer and never kill the thread... Maybe you want to do something that can be do in another way???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
Reply With Quote
  #9 (permalink)  
Old April 3rd, 2008, 02:39 PM
Authorized User
 
Join Date: Sep 2007
Location: , , Finland.
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you tell us what you are trying to do with that thread? There could be better ways to solve your problem.

Threads are horrible little things that should be avoided at almost all cost and they are especially horrible in stateless stuff like ASP.NET.
Reply With Quote
  #10 (permalink)  
Old April 3rd, 2008, 08:47 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Threads are not horrible things, but you really need to know how to use them..

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Thread to use and How peace2007 ASP.NET 1.0 and 1.1 Professional 0 September 23rd, 2007 03:24 AM
abort Asynchron? ramo9941 C# 1 May 3rd, 2007 07:07 AM
Abort thread when regular expression get stuck? gurupak C# 2005 0 March 20th, 2007 02:13 AM
Problem in Thread Atif Khan C# 0 April 8th, 2005 06:57 AM
terminate a thread problem lmadhavi Visual C++ 0 November 23rd, 2004 05:20 AM



All times are GMT -4. The time now is 06:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.