473,800 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the best way to kill a thread in a Timer?

108 New Member
What is the best way to kill a thread in a timer such that every time the timer runs, it kills the previous thread before starting a new one?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class MapTimer extends TimerTask{
  3.  
  4. public void run() {
  5.  
  6.  
  7.         try {
  8.  
  9.             connected = startConnection();
  10.             sendMessage(this.message);
  11.             System.out.println("Sending message111 ..."+this.message);
  12.             serverquerystring=receiveMessage();
  13.  
  14.             thread = new Thread() {
  15.             public void run() {
  16.                     try {
  17.                          System.out.println("In run..");
  18.  
  19.                         checkquery(serverquerystring);
  20.                     } catch (Exception ex) {
  21.                         ex.printStackTrace();
  22.                     }
  23.             }
  24.     };
  25.     thread.start(); // write post-action user code here
  26.     }
  27.  
  28.  
  29.           catch(Exception e){
  30.             System.out.println("Error "+e);
  31.        }
  32.  
  33. }
  34. }
  35.  
May 20 '11 #1
1 1653
Plater
7,872 Recognized Expert Expert
Keep a reference to the currently active thread (some Thread variable) when your timer executes, tell the thread to stop. (See tutorials on best practices for exiting the thread) You can then do a .Join() (with a timeout so that you can call .Abort() if need be)
Then start your new thread when the previous one exits?
May 23 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
11229
by: Dhananjay Singh | last post by:
Dear All, There are lot of questions in mind regarding Green Thread. I'd like to know what is green thread? What is the difference between Green Thread and Native Thread? On linux platform which one is good and why "java -green" command is not working on the linux (using j2sdk1.4.2_05 for linux). And above all of these question ......... Why java.sun.com doesn't have any good article for green threads. if u
7
1389
by: Mr. x | last post by:
now it is 18:16.
9
15288
by: Brett | last post by:
I'm trying to kill a thread spawned this way: Form1 spawns Class1 via Thread.start() Here's my code to kill the thread: If (t.ThreadState.ToString = "SuspendedRequested, WaitSleepJoin") Or (t.ThreadState.ToString = "Suspended") Or (t.ThreadState.ToString = "WaitSleepJoin, Suspended") Then Else t.Abort()
3
2876
by: Thomas Dybdahl Ahle | last post by:
Hi, I'm writing an application that connects to the internet. Something like this: for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: self.sock = socket.socket(af, socktype, proto) Now if the user press the cancel button, I'd like the connection to imidiatly stop. I run
3
1493
by: chance | last post by:
I want my thread method to return a String. However, the compiler is saying that the best overloaded method match for System.Threading.Thread has some invalid arguments. Not sure what I need to change here. public String generateNOV(string xmlParms) { Thread novThread = new Thread(delegate() tia,
5
2686
by: Teja | last post by:
Hi all, Can any on help me out in killing a thread (i.e deleteing the reources like, stack ,memory etc) which is started with win32process.beginthreadex()??? Rite now, I am suspending the thread. But any pointers as to how to delete the thread permanently? Its pretty urgent... Please...
3
1482
by: babutime | last post by:
What is green thread
4
5166
by: liu yang | last post by:
I want to build a timer for each thread, and the timer must be thread- safe. How to do that? My environment is Linux and Pthread. Thanks.
4
2377
by: Mathieu Prevot | last post by:
Hi, I have a threading.Thread class with a "for i in range(1,50)" loop within. When it runs and I do ^C, I have the error as many as loops. I would like to catch this exception (and if possible do some cleanup like in C pthreads) so the program finishes cleanly. Where and how can I do this ? in __run__ ? __init__ ? a try/except stuff ? Thanks, Mathieu
0
1264
by: siddhartha92k | last post by:
sir , please tell me what is mail thread.in any lotus mail & other mail like gmail etc.
0
9690
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
9551
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
10505
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
10033
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
9085
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
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
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
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
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

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.