473,769 Members | 3,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to kill a thread?

Hi,
I have a windows form application, and there are 2 buttons in the form. In
first button's click event I have code like:
Thread t = new Thread(new ThreadStart(fil eProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.

How can I do it?

Thanks
Hardy
Nov 16 '05 #1
11 4272
Hi Hardy,

One way would be to call Thread.Abort() method, though better way is to use
some sort of synchronization mechanims, such as ManualEvent.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Hardy Wang" <ha*******@hotm ail.com> wrote in message
news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
Hi,
I have a windows form application, and there are 2 buttons in the form. In
first button's click event I have code like:
Thread t = new Thread(new ThreadStart(fil eProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.

How can I do it?

Thanks
Hardy

Nov 16 '05 #2
Thanks,
The definition of thread appears in first button's click event. In the
second button's click event, thread definition is out of scope, so how can I
call Thread.Abort() in second function?

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:eS******** ******@tk2msftn gp13.phx.gbl...
Hi Hardy,

One way would be to call Thread.Abort() method, though better way is to use some sort of synchronization mechanims, such as ManualEvent.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Hardy Wang" <ha*******@hotm ail.com> wrote in message
news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
Hi,
I have a windows form application, and there are 2 buttons in the form. In first button's click event I have code like:
Thread t = new Thread(new ThreadStart(fil eProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.
How can I do it?

Thanks
Hardy


Nov 16 '05 #3

"Hardy Wang" <ha*******@hotm ail.com> wrote in message
news:ew******** ******@TK2MSFTN GP10.phx.gbl...
Thanks,
The definition of thread appears in first button's click event. In the
second button's click event, thread definition is out of scope, so how can I call Thread.Abort() in second function?

Don't define it in the second button's event. Define it as a member of your
class.
Nov 16 '05 #4
You would need to define the thread higher in scope (like at the class
level) so that it can be accessed or create a custom class that has access
to the thread internally.

public class MyThread
{
private Thread myThread;

public MyThread()
{
myThread = new Thread(new ThreadStart(fil eProcessor));
}

public void StartThread()
{
myThread.Start( );
}

public void StopThread()
{
myThread.Abort( );
}
}
--
HTH

Kyril Magnos
"I'm not a developer anymore, I'm a software engineer now!" :-)

"Hardy Wang" <ha*******@hotm ail.com> wrote in message
news:ew******** ******@TK2MSFTN GP10.phx.gbl...
| Thanks,
| The definition of thread appears in first button's click event. In the
| second button's click event, thread definition is out of scope, so how can
I
| call Thread.Abort() in second function?
|
| "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
| news:eS******** ******@tk2msftn gp13.phx.gbl...
| > Hi Hardy,
| >
| > One way would be to call Thread.Abort() method, though better way is to
| use
| > some sort of synchronization mechanims, such as ManualEvent.
| >
| > --
| > Miha Markic [MVP C#] - RightHand .NET consulting & development
| > miha at rthand com
| > www.rthand.com
| >
| > "Hardy Wang" <ha*******@hotm ail.com> wrote in message
| > news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
| > > Hi,
| > > I have a windows form application, and there are 2 buttons in the
form.
| In
| > > first button's click event I have code like:
| > > Thread t = new Thread(new ThreadStart(fil eProcessor));
| > > t.Start();
| > > To run a lengthy processing.
| > >
| > > I hope I can manully terminate this thread when I click the second
| button.
| > >
| > > How can I do it?
| > >
| > > Thanks
| > > Hardy
| > >
| > >
| >
| >
|
|
Nov 16 '05 #5
Thanks
"Kyril Magnos" <ky**********@y ahoo.com> wrote in message
news:un******** ******@tk2msftn gp13.phx.gbl...
You would need to define the thread higher in scope (like at the class
level) so that it can be accessed or create a custom class that has access
to the thread internally.

public class MyThread
{
private Thread myThread;

public MyThread()
{
myThread = new Thread(new ThreadStart(fil eProcessor));
}

public void StartThread()
{
myThread.Start( );
}

public void StopThread()
{
myThread.Abort( );
}
}
--
HTH

Kyril Magnos
"I'm not a developer anymore, I'm a software engineer now!" :-)

"Hardy Wang" <ha*******@hotm ail.com> wrote in message
news:ew******** ******@TK2MSFTN GP10.phx.gbl...
| Thanks,
| The definition of thread appears in first button's click event. In the | second button's click event, thread definition is out of scope, so how can I
| call Thread.Abort() in second function?
|
| "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
| news:eS******** ******@tk2msftn gp13.phx.gbl...
| > Hi Hardy,
| >
| > One way would be to call Thread.Abort() method, though better way is to | use
| > some sort of synchronization mechanims, such as ManualEvent.
| >
| > --
| > Miha Markic [MVP C#] - RightHand .NET consulting & development
| > miha at rthand com
| > www.rthand.com
| >
| > "Hardy Wang" <ha*******@hotm ail.com> wrote in message
| > news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
| > > Hi,
| > > I have a windows form application, and there are 2 buttons in the
form.
| In
| > > first button's click event I have code like:
| > > Thread t = new Thread(new ThreadStart(fil eProcessor));
| > > t.Start();
| > > To run a lengthy processing.
| > >
| > > I hope I can manully terminate this thread when I click the second
| button.
| > >
| > > How can I do it?
| > >
| > > Thanks
| > > Hardy
| > >
| > >
| >
| >
|
|

Nov 16 '05 #6
yw :-)

--
HTH

Kyril Magnos
"I'm not a developer anymore, I'm a software engineer now!" :-)

"Hardy Wang" <ha*******@hotm ail.com> wrote in message
news:eu******** ******@TK2MSFTN GP09.phx.gbl...
| Thanks
| "Kyril Magnos" <ky**********@y ahoo.com> wrote in message
| news:un******** ******@tk2msftn gp13.phx.gbl...
| > You would need to define the thread higher in scope (like at the class
| > level) so that it can be accessed or create a custom class that has
access
| > to the thread internally.
| >
| > public class MyThread
| > {
| > private Thread myThread;
| >
| > public MyThread()
| > {
| > myThread = new Thread(new ThreadStart(fil eProcessor));
| > }
| >
| > public void StartThread()
| > {
| > myThread.Start( );
| > }
| >
| > public void StopThread()
| > {
| > myThread.Abort( );
| > }
| > }
| >
| >
| > --
| > HTH
| >
| > Kyril Magnos
| > "I'm not a developer anymore, I'm a software engineer now!" :-)
| >
| > "Hardy Wang" <ha*******@hotm ail.com> wrote in message
| > news:ew******** ******@TK2MSFTN GP10.phx.gbl...
| > | Thanks,
| > | The definition of thread appears in first button's click event. In
| the
| > | second button's click event, thread definition is out of scope, so how
| can
| > I
| > | call Thread.Abort() in second function?
| > |
| > | "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
| > | news:eS******** ******@tk2msftn gp13.phx.gbl...
| > | > Hi Hardy,
| > | >
| > | > One way would be to call Thread.Abort() method, though better way is
| to
| > | use
| > | > some sort of synchronization mechanims, such as ManualEvent.
| > | >
| > | > --
| > | > Miha Markic [MVP C#] - RightHand .NET consulting & development
| > | > miha at rthand com
| > | > www.rthand.com
| > | >
| > | > "Hardy Wang" <ha*******@hotm ail.com> wrote in message
| > | > news:Oi******** ******@TK2MSFTN GP10.phx.gbl...
| > | > > Hi,
| > | > > I have a windows form application, and there are 2 buttons in the
| > form.
| > | In
| > | > > first button's click event I have code like:
| > | > > Thread t = new Thread(new ThreadStart(fil eProcessor));
| > | > > t.Start();
| > | > > To run a lengthy processing.
| > | > >
| > | > > I hope I can manully terminate this thread when I click the second
| > | button.
| > | > >
| > | > > How can I do it?
| > | > >
| > | > > Thanks
| > | > > Hardy
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| >
| >
|
|
Nov 16 '05 #7
Hardy Wang <ha*******@hotm ail.com> wrote:
I have a windows form application, and there are 2 buttons in the form. In
first button's click event I have code like:
Thread t = new Thread(new ThreadStart(fil eProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second button.


See
http://www.pobox.com/~skeet/csharp/m...worker.threads

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Hardy Wang <ha*******@hotm ail.com> wrote:
The definition of thread appears in first button's click event. In the
second button's click event, thread definition is out of scope, so how can I
call Thread.Abort() in second function?


I strongly recommend *not* using Thread.Abort. It can leave your data
in a nasty state. For example, suppose you have a method which modifies
two fields and keeps them consistent: if you abort the thread half way
through the method, the data (which may be accessible in another
thread) could be inconsistent.

Tell the thread you want it to stop gracefully at the next appropriate
point instead (see my other post in this thread for a link).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Thanks!!

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Hardy Wang <ha*******@hotm ail.com> wrote:
I have a windows form application, and there are 2 buttons in the form. In first button's click event I have code like:
Thread t = new Thread(new ThreadStart(fil eProcessor));
t.Start();
To run a lengthy processing.

I hope I can manully terminate this thread when I click the second
button.
See
http://www.pobox.com/~skeet/csharp/m...worker.threads

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10

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

Similar topics

12
18898
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering if there is a simple way from a main python program to kill a running thread? I see with the 'threading' module the way daemonic threads behave when the main program finishes.
5
10940
by: Blatwurst | last post by:
I'm trying to implement a simple server in C#. I want to do the classic thing of spinning off a thread that just blocks in a Socket.Accept() call until a request comes in. At that point, the Accept() returns, the thread spins off another thread to handle the request, and then calls Accept() again. This all works fine except that I can find no way to kill the thread that is blocked in the Accept() call when I want to shut down the server. ...
6
42878
by: RickDee | last post by:
Understand that when I start a thread, a number will be generated and is able to get from GetHashCode method. But I would like to use this number when I want to kill certain thread, anybody know how ?? Thanks Regards
3
5857
by: Stewart | last post by:
Hey Group, Hoping someone can help me out. I have some code which starts up some asynchronous code using a delegate. The code is below. Basically my main code (not shown) calls ServerThreadStart.StartServer to start the server running asynchronously. This works fine. Shouldn't be any problems here. My question is how can I get my code to kill this code running asynchronously? There is a dlgtServer.Remove(Delegate, Delegate)
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()
2
3508
by: Christopher Carnahan | last post by:
I need to figure out how to terminate a thread while it is blocked trying to create a COM object via interop. In a worker thread, I do something like this: Type t = null; Object activatedObject = null; Legacy.IScheduled comObject = null; t = Type.GetTypeFromProgID(ProgID);
5
10600
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has not given me any hints on how to cleanly kill running threads before exiting. Any help is appreciated! Carl
18
10245
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
20
5099
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
John Dohn wrote: When I do this, I put a special value in the queue (like None) and in the worker thread, check for the special value and exit if found. Threads can also be marked as "daemon threads" (see docs for threading.Thread objects). This will make the application terminate if only "daemon threads" are left. So best would probably be soemthing like
1
10393
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, Is it possible "kill" the thread of Backgroundworker ? In my Dowork event, I have NOT While for do e.Cancel = true, only have a call to external COM. If I want cancel, calling CancelAsync, not cancels the call to COM. How I can do it , please ? Any suggestions will be very appreciated.
0
9589
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
9423
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
10216
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...
1
9997
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,...
1
7413
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
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.