PC Review


Reply
Thread Tools Rate Thread

calling exit thread from other thread ?

 
 
Daylor
Guest
Posts: n/a
 
      28th Jul 2003
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 ?


 
Reply With Quote
 
 
 
 
Ryan Byington
Guest
Posts: n/a
 
      31st Jul 2003
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" <(E-Mail Removed)>
>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: <(E-Mail Removed)>
>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!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!in.100proofnews.com!in.
100proofnews.com!seanews2.seabone.net!news.012.net.il
>Xref: 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

created
>on t1 ,will call exitthread to the main thread)
>
>what is the simple way to do that ?
>
>
>


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Main thread calling event from child thread without using Ctrl.Inv =?Utf-8?B?Tm91bG91aw==?= Microsoft Dot NET Framework Forms 7 5th Jul 2006 01:58 AM
Calling a main thread method from a worker thread Hao L Microsoft C# .NET 5 2nd May 2005 08:31 PM
Calling Thread.Suspend and it's affect on the target thread... Paul Tomlinson Microsoft C# .NET 1 3rd Feb 2005 11:14 PM
Calling Main Thread from a worker thread in vb.net Documentation Peter Microsoft VB .NET 1 1st Nov 2004 07:22 PM
Thread Pool Thread error exit John Nauman Microsoft Dot NET 0 20th Jan 2004 06:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:16 PM.