473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(h ost);

// 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(50 00);

// ????
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 1566
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 MyMemberVisible Thread;

private/public/protected void CreateMyThread( ){

this.MyMemberVi sibleThread = new Thread();
....
...
..

}

private void Timer5SecondsEl apsedEventHandl er(...){

try{

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

}catch(Exceptio n 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*****@nospam pleasewearebrit ish.easy.comsai d:
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(thr eadWork.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(h ost);

// 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(50 00);

// ????
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
4382
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 some inserts will fail due to missing information or that a record already exists for it. One of the main reasons for me using the cursor is so that ALL the data is not rolled back when this occurrs. But it seems that when I run the job, the...
1
2624
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 the expected behavior for this scenario? Does the thread stay alive until the callback is executed? If an exception is thrown, can it be caught? I posted this message last week, but got no response. I am really hoping someone can help me with...
2
2046
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(...) { ....doing some work Monitor.Enter(Thread_1);
1
1223
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 thread is performing these actions with a "cancel" button on it. My question is, at what point does the thread actually stop running after I have executed "Abort" in response to the user pressing cancel? Is it immediate? Or does the thread...
1
1647
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 correspondent to the selected Item. The reason for using threads here is speed (since the filling of the form takes +/- 0.5 sec). But if I rapidly select some different items in the listbox (e.g. with the arrow buttons), the application stops executing, and...
2
1403
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 the app closes down to close all running threads. What's the best way to achieve this without running into problems?
7
6807
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 period of time. I want something like thread.abort for aborting aynchronous call. Can someone tell me way of aborting asynchronous call.
2
1615
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 socket. However I have been having network issues and have found the code is aborting whenever the network has issues. Is there a way to keep the code from aborting? here is the snippet that seems to cause the code to abort. use File::Tail; use...
7
3270
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 boolean called KillAllThreads (public static bool in abstact class). I initialize it to false in my main program before my main window opens. I then have some threads open and close. I keep count of opened and closed threads by using another...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.