Wrox Programmer Forums
|
.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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 3rd, 2008, 08:01 AM
Authorized User
 
Join Date: Apr 2008
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
 
Old April 3rd, 2008, 08:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 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 -/
 
Old April 3rd, 2008, 08:24 AM
Authorized User
 
Join Date: Apr 2008
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
 
Old April 3rd, 2008, 08:27 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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

/- Sam Judson : Wrox Technical Editor -/
 
Old April 3rd, 2008, 08:36 AM
Authorized User
 
Join Date: Apr 2008
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
 
Old April 3rd, 2008, 08:40 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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

See point 3.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 3rd, 2008, 08:50 AM
Authorized User
 
Join Date: Apr 2008
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
 
Old April 3rd, 2008, 09:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
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
================================================== =========
 
Old April 3rd, 2008, 02:39 PM
Authorized User
 
Join Date: Sep 2007
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.
 
Old April 3rd, 2008, 08:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
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
================================================== =========





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 06:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.