How to kill a thread?

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
 
M

Miha Markic [MVP C#]

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

Hardy Wang

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
 
M

Matt Burland

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

Kyril Magnos

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
| > >
| > >
| >
| >
|
|
 
K

Kyril Magnos

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
| > | > >
| > | > >
| > | >
| > | >
| > |
| > |
| >
| >
|
|
 
J

Jon Skeet [C# MVP]

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

Miha Markic [MVP C#]

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

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top