473,411 Members | 1,998 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,411 software developers and data experts.

calling exit thread from other thread ?

hi.
i have main thread , and 2 others thread (t1 ,t2 for example)

how can i call from the main thread, to tell t1 to exit his thread ?
(calling application.exitthread from main thread, in method of class created
on t1 ,will call exitthread to the main thread)

what is the simple way to do that ?
Jul 19 '05 #1
1 3516
There is a couple of ways you can handle this.

One you can you call t1.Abort() or t2.Abort() and the thread will exit.
This is a pretty abrupt shutdown of the thread and may leave some of the
references the thread has in an undefined state.

The other option is to have some property that the helper thread checks and
when set to true by the main thread the helper thread exits.

In the main thread do the following:

MyWorkerThread myWorker = new MyWorkerThread()
Thread t = new Thread(new ThreadStart(myWorker.DoSomeWork));
t.Start();

/*
DO SOME STUFF
*/

myWorker.ExitThread = true;
/* The thread should now exit on the next iteration */
public class MyWorkerThread
{
public bool ExitThread = false;

public void DoSomeWork()
{
while(true) {
if(ExitThread) {
return;
}
/* DO SOME WORK HERE */
}
}
}

Thanks,

Ryan Byington [MS]

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
--------------------
From: "Daylor" <Da****@012.net.il>
Newsgroups: microsoft.public.dotnet.general
Subject: calling exit thread from other thread ?
Date: Mon, 28 Jul 2003 21:27:50 +0200
Lines: 10
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 80.179.5.179.forward.012.net.il
Message-ID: <3f******@news.012.net.il>
X-Trace: news.012.net.il 1059416881 80.179.5.179.forward.012.net.il (28 Jul 2003 21:28:01 +0300)Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!in.100p roofnews.com!in.
100proofnews.com!seanews2.seabone.net!news.012.net .ilXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:102609
X-Tomcat-NG: microsoft.public.dotnet.general

hi.
i have main thread , and 2 others thread (t1 ,t2 for example)

how can i call from the main thread, to tell t1 to exit his thread ?
(calling application.exitthread from main thread, in method of class createdon t1 ,will call exitthread to the main thread)

what is the simple way to do that ?


Jul 19 '05 #2

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

Similar topics

1
by: Hank | last post by:
Hi, I have the following: def readuser(): ...check for user input then exit entire script thread.start_new_thread(readuser,()) while(forever):
3
by: Maxwell Hammer | last post by:
An application I am developing executes many threads and then has a "monitor" part that waits for certain events. One of these events causes the application to have to shutdown. On shutdown the...
4
by: Bryan | last post by:
Quick question: Why does os._exit called from a Python Timer kill the whole process while sys.exit does not? On Suse. Bryan
5
by: bughunter | last post by:
Hi, Consider this code: ---- Monitor.Pulse(oLock); Monitor.Exit(oLock); ---- If a thread was waiting on oLock then will the current thread
0
by: szehau | last post by:
Hi all, I have a program written in C with embeded SQL. Following are the configuration: DB2/LINUX 8.1.5 Thread model: posix gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) My problems...
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
1
by: Daylor | last post by:
hi. i have main thread , and 2 others thread (t1 ,t2 for example) how can i call from the main thread, to tell t1 to exit his thread ? (calling application.exitthread from main thread, in method...
2
by: ramasubramanian.rahul | last post by:
hi i am trying to call some java APIs from c . i use the standatd JNI calls to load the JVM from a c program and call all java functions by using a pointer to the jvm which was returned by the JNI...
3
by: jrpfinch | last post by:
I have a script which is based on the following code. Unfortunately, it only works on Python 2.3 and not 2.5 because there is no esema or fsema attribute in the 2.5 Queue. I am hunting through...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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...

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.