473,412 Members | 3,471 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,412 software developers and data experts.

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(fileProcessor));
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 4244
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*******@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP10.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(fileProcessor));
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**************@tk2msftngp13.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*******@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP10.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(fileProcessor));
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*******@hotmail.com> wrote in message
news:ew**************@TK2MSFTNGP10.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(fileProcessor));
}

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*******@hotmail.com> wrote in message
news:ew**************@TK2MSFTNGP10.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**************@tk2msftngp13.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*******@hotmail.com> wrote in message
| > news:Oi**************@TK2MSFTNGP10.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(fileProcessor));
| > > 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**********@yahoo.com> wrote in message
news:un**************@tk2msftngp13.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(fileProcessor));
}

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*******@hotmail.com> wrote in message
news:ew**************@TK2MSFTNGP10.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**************@tk2msftngp13.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*******@hotmail.com> wrote in message
| > news:Oi**************@TK2MSFTNGP10.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(fileProcessor));
| > > 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*******@hotmail.com> wrote in message
news:eu**************@TK2MSFTNGP09.phx.gbl...
| Thanks
| "Kyril Magnos" <ky**********@yahoo.com> wrote in message
| news:un**************@tk2msftngp13.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(fileProcessor));
| > }
| >
| > 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*******@hotmail.com> wrote in message
| > news:ew**************@TK2MSFTNGP10.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**************@tk2msftngp13.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*******@hotmail.com> wrote in message
| > | > news:Oi**************@TK2MSFTNGP10.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(fileProcessor));
| > | > > 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*******@hotmail.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(fileProcessor));
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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Hardy Wang <ha*******@hotmail.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Hardy Wang <ha*******@hotmail.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(fileProcessor));
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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10
Hi Jon,

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Hardy Wang <ha*******@hotmail.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.


Yup, that's my reasoning too. However, there are times that Abort might be
useful.
Imagine doing a long Fill of a datatable which you want to abort at some
time...

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).


Or use Abort with great care :-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com
Nov 16 '05 #11
<"Miha Markic [MVP C#]" <miha at rthand com>> wrote:
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.


Yup, that's my reasoning too. However, there are times that Abort might be
useful.
Imagine doing a long Fill of a datatable which you want to abort at some
time...


Close the connection from another thread, perhaps? Not sure whether
that would work or not...
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).


Or use Abort with great care :-)


Hmm. Easier said than done, IMO.

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

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

Similar topics

12
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...
5
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...
6
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...
3
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...
9
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...
2
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...
5
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...
18
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...
20
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...
1
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.