How to kill a thread?

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

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
 
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.
 
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 said:
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 said:
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
 
Hardy Wang said:
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.
 
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!" :-)

| 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
| | > 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
| >
| > | > > 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
| > >
| > >
| >
| >
|
|
 
yw :-)

--
HTH

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

| Thanks
| | > 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!" :-)
| >
| > | > | 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
| > | | > | > 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
| > | >
| > | > | > | > > 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
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| >
| >
|
|
 
Hardy Wang said:
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).
 
Hi Jon,

Jon Skeet said:
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 :-)
 
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...
Or use Abort with great care :-)

Hmm. Easier said than done, IMO.
 
Back
Top