473,672 Members | 2,676 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 1558
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
4373
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
2615
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
2035
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
1219
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
1639
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
1395
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
6800
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
1608
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
3261
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
8486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8828
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
8608
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
8680
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
7446
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...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
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.