473,326 Members | 2,813 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,326 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 3508
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.