473,399 Members | 3,401 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,399 software developers and data experts.

Aborting Threads

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,
Nov 30 '07 #1
3 1540
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."
Nov 30 '07 #2
On 2007-11-30 10:43:57 -0800, "AdrianDev"
<ad*****@nospampleasewearebritish.easy.comsaid:
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

Nov 30 '07 #3
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,
Nov 30 '07 #4

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

Similar topics

0
by: Jason | last post by:
I have a job created on my server which runs a stored procedure. The SP uses a cursor on a recordset to insert rows into a table (via a stored procedure - hence the cursor). It is expected that...
1
by: johnny | last post by:
In a multi-threaded application, say a worker thread makes an asynchronous call and specifies a callback method. But before the callback is executed, the thread is aborted by its creator. What is...
2
by: Xarky | last post by:
Hi, I am writing a small program, that makes use of threads. Now in on of the threads I have a critical section, where I am using the Monitor to handle this. *** Thread_1 *** started for(...)...
1
by: Robin Tucker | last post by:
Hi there, I have a "worker thread", which can perform one of many tasks, including fetching and sending data blobs to a database, load files etc. Now, a progress dialog is displayed while the...
1
by: Snuyt | last post by:
Hello, I'm using threads to fill a form with multiple controls: In a listbox.selecteditem_changed, I abort the current thread, and I start a new thread to fill the form with the data...
2
by: Mantorok | last post by:
Hi all I have an app that can kick off one or more threads, a thread will run a COM EXE Server. What I want to do is provide the facility to cancel a thread that is running, and also, when...
7
by: archana | last post by:
Hi all, I am having application in which i am doing asynchronous call.I am using manualresetevent to wait for asynchronous call to complete. I want to stop asynchronous call after certain...
2
by: cookspyder | last post by:
I have a simple socket client code that sends the contents of a syslog over to a server when it finds an error. It has been working fine in a while loop of read open socket send line and close...
7
by: matthewaveryusa | last post by:
I have been reading a lot about aborting threads and there seems to be something missing to do this easily on program exit. This is what I want to do but I will need some help. I declared a global...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.