PC Review


Reply
Thread Tools Rate Thread

Aborting Threads

 
 
AdrianDev
Guest
Posts: n/a
 
      30th Nov 2007
Hi,

I have a thread which I call like this:

// Allocate a new thread containing class with the host
getInfoThread git = new getInfoThread(host);

// Create the thread and call the Go method
new Thread(new ThreadStart (git.Go)).Start();

// Sleep for 5 seconds and abort the Thread if still active
//Thread,Sleep(5000);

// ????


I want to abort the thread if it has'nt completed after 5 seconds. But how
can I get a reference to the created Thread?


thanks,


 
Reply With Quote
 
 
 
 
Kerem Gümrükcü
Guest
Posts: n/a
 
      30th Nov 2007
Hi Adrian,

you should declare a thread in the scope of your class, so that
its members can access it an terminate/modify if necessary. The
Termination can be done inside a timer that also should catch
the ThreadAbort Exception. Simple try/catch directive.

Example:

public class SomeClass{

private Thread MyMemberVisibleThread;

private/public/protected void CreateMyThread(){

this.MyMemberVisibleThread = new Thread();
....
...
..

}

private void Timer5SecondsElapsedEventHandler(...){

try{

this.MyMemberVisibleThread.Abort();
//i am not sure whether it was abort() or terminate() to stop
the
//thread, check for the right method

}catch(Exception e1){

//Handle exception here

}

}


}

Regards

Kerem


--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space: http://kerem-g.spaces.live.com/
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."


 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      30th Nov 2007
On 2007-11-30 10:43:57 -0800, "AdrianDev"
<(E-Mail Removed)> said:

> I have a thread which I call like this: [...]
>
> // Create the thread and call the Go method
> new Thread(new ThreadStart (git.Go)).Start();
>
> [...] I want to abort the thread if it has'nt completed after 5
> seconds. But how
> can I get a reference to the created Thread?


At the risk of helping you manage a thread in exactly the wrong way, I
will point out that you had a reference to the created thread when you
called "new Thread()". You just didn't bother to save it into some
variable.

But don't abort threads. If you need a way to have a thread terminated
after some period of time, you need to either build the timeout into
the thread logic itself, or you should provide a more general-purpose
signal to the thread to inform it to exit its processing (e.g. a
volatile flag).

Aborting a thread is no way to manage your code.

Pete

 
Reply With Quote
 
Peter Bromberg [C# MVP]
Guest
Posts: n/a
 
      30th Nov 2007
Thread newThread =
new Thread(new ThreadStart(threadWork.DoWork));

The variable "newThread" of type Thread is your reference to the new Thread
you have created. You can then call its Start method.
--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com



"AdrianDev" wrote:

> Hi,
>
> I have a thread which I call like this:
>
> // Allocate a new thread containing class with the host
> getInfoThread git = new getInfoThread(host);
>
> // Create the thread and call the Go method
> new Thread(new ThreadStart (git.Go)).Start();
>
> // Sleep for 5 seconds and abort the Thread if still active
> //Thread,Sleep(5000);
>
> // ????
>
>
> I want to abort the thread if it has'nt completed after 5 seconds. But how
> can I get a reference to the created Thread?
>
>
> thanks,
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread Local Storage and Managed Threads vs O/S Threads Chris Mullins [MVP] Microsoft Dot NET Framework 5 3rd Mar 2007 09:48 PM
Aborting threads Mantorok Microsoft C# .NET 2 2nd May 2006 12:48 PM
Aborting threads before quit my application using Application.Exit =?Utf-8?B?VGhhbnlhIFRldXRzY2hiZWlt?= Microsoft Dot NET Compact Framework 24 2nd Mar 2005 03:22 PM
Aborting Threads Glyn Meek Microsoft Dot NET Framework 8 23rd Nov 2004 03:17 AM
is Aborting threads really OK? Jim Microsoft Dot NET Framework 2 27th Feb 2004 07:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:21 PM.