473,388 Members | 1,524 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,388 software developers and data experts.

Threading: Stopping, restarting, etc

I'm having a problem with my Thread usage and I think the general design of
how I'm working with them. My UI class calls a method in another class that
does a lot of work. That "worker" class looks something like this(pseudo
code):
class WorkerClass
{
Thread _listenerThread;

public WorkerClass()
{
_listenerThread = new Thread( new ThreadStart(ListenForInput) );
_listenerThread.IsBackground = true;
_listenerThread.Name = "Worker Listener Thread";
}

public void StartTest()
{
// do some quick stuff, then start the listener thread
_listenerThread.Start();
}

private void ListenForInput()
{
// make call to external library that
// won't return until a condition is met
}

public void CancelTest()
{
_listenerThread.Stop();
}
}
That is pretty close to what I have. So, I have a couple issues that I'm
dealing with. If I have run StartTest() once and call it again later (I
handle when it can be called, ListenForInput raises an event when it's done)
the _listenerThread's ThreadState is 'Background' so a subsequent call to
Start() throws an exception.

I figured I was having this behavior because the thread is a member of the
class rather than local to a method, but I need to have it as a member so
that I can cancel it from the UI if a user wishes.

I added some code to the end of ListenForInput() that would call Stop() on
the thread, but that seems dangerous to kill the thread you are running on?
In fact, is it safe for a process to modify ANYTHING about a thread it's
running on? Like priority or ThreadState?

Basically I'm just not sure how to design what I need. The above class is
an accurate model of what I'm after. Most of the threading examples I have
seen online or too simple and don't cover stuff like this.

Thanks for reading, I hope someone has some pointers for me.
-Steve
May 2 '06 #1
11 1834

Steve wrote:
I'm having a problem with my Thread usage and I think the general design of
how I'm working with them. My UI class calls a method in another class that
does a lot of work. [snip] Basically I'm just not sure how to design what I need. The above class is
an accurate model of what I'm after. Most of the threading examples I have
seen online or too simple and don't cover stuff like this.


Immediately go and read Jon Skeet's Threading pages at
<http://yoda.arachsys.com/csharp/threads>, or if feeling lazy, just the
'Shutting down worker threads gracefully' page at
<http://yoda.arachsys.com/csharp/threads/shutdown.shtml>.

An invaluable resource!

--
Larry Lard
Replies to group pleas

May 2 '06 #2
Steve,

I think you are going about this the wrong way. You should rarely call
Stop on a thread in order to stop processing on it. Rather, you are better
off having some sort of shared variable which the thread will check
periodically to see if it should terminate. If the variable is set, then
the code in the thread will just exit, and the thread will die.

Also, you should create a new thread when you call StartTest, not when
you create your class. You should use a new thread each time. You can
choose to store it on the class level, but the only reason I can see doing
so would be to determine if an operation is running.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:uR*************@TK2MSFTNGP05.phx.gbl...
I'm having a problem with my Thread usage and I think the general design
of how I'm working with them. My UI class calls a method in another class
that does a lot of work. That "worker" class looks something like
this(pseudo code):
class WorkerClass
{
Thread _listenerThread;

public WorkerClass()
{
_listenerThread = new Thread( new ThreadStart(ListenForInput) );
_listenerThread.IsBackground = true;
_listenerThread.Name = "Worker Listener Thread";
}

public void StartTest()
{
// do some quick stuff, then start the listener thread
_listenerThread.Start();
}

private void ListenForInput()
{
// make call to external library that
// won't return until a condition is met
}

public void CancelTest()
{
_listenerThread.Stop();
}
}
That is pretty close to what I have. So, I have a couple issues that I'm
dealing with. If I have run StartTest() once and call it again later (I
handle when it can be called, ListenForInput raises an event when it's
done) the _listenerThread's ThreadState is 'Background' so a subsequent
call to Start() throws an exception.

I figured I was having this behavior because the thread is a member of the
class rather than local to a method, but I need to have it as a member so
that I can cancel it from the UI if a user wishes.

I added some code to the end of ListenForInput() that would call Stop() on
the thread, but that seems dangerous to kill the thread you are running
on? In fact, is it safe for a process to modify ANYTHING about a thread
it's running on? Like priority or ThreadState?

Basically I'm just not sure how to design what I need. The above class is
an accurate model of what I'm after. Most of the threading examples I
have seen online or too simple and don't cover stuff like this.

Thanks for reading, I hope someone has some pointers for me.
-Steve

May 2 '06 #3
Cool, I found the first link while googling, but the second one about
stopping is interesting. At first glance, it would seem that it's best
suited for an iterative process that will have a chance to check for the
stop flag. My thread makes a call to a function that will not return until
a specific condition is met. So in theory, it could sit there waiting for
days.

I'm not happy with the fact that it operates that way or doesn't have a
timeout parameter, but it's not my library and I can't change it.

John's article gave me more of an understanding, but I'm still not seeing a
clear path how to proceed forward. I will keep reading some more and
experimenting.

Thanks for the post,
Steve
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11********************@y43g2000cwc.googlegrou ps.com...

Steve wrote:
I'm having a problem with my Thread usage and I think the general design
of
how I'm working with them. My UI class calls a method in another class
that
does a lot of work.

[snip]
Basically I'm just not sure how to design what I need. The above class
is
an accurate model of what I'm after. Most of the threading examples I
have
seen online or too simple and don't cover stuff like this.


Immediately go and read Jon Skeet's Threading pages at
<http://yoda.arachsys.com/csharp/threads>, or if feeling lazy, just the
'Shutting down worker threads gracefully' page at
<http://yoda.arachsys.com/csharp/threads/shutdown.shtml>.

An invaluable resource!

--
Larry Lard
Replies to group pleas

May 2 '06 #4
Hi Nicholas,

I agree that I should not create the thread in the ctor, I changed that
after reading your post. I also understand what you mean about the stop
flag being checked, it's like Jon Skeet's article that Larry posted.

My situation is maybe a bit unique in that my thread is stuck in a method
call that can't check for the stop flag. I would like to kill the thread
gracefully, but I'm not sure if I can.

Basically my situation is:
Thread t = new Thread( new ThreadStart(LongMethod) );
t.Start();

private void LongMethod()
{
while(true)
{
//...
}
}
Any other suggestions?

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eK**************@TK2MSFTNGP02.phx.gbl...
Steve,

I think you are going about this the wrong way. You should rarely call
Stop on a thread in order to stop processing on it. Rather, you are
better off having some sort of shared variable which the thread will check
periodically to see if it should terminate. If the variable is set, then
the code in the thread will just exit, and the thread will die.

Also, you should create a new thread when you call StartTest, not when
you create your class. You should use a new thread each time. You can
choose to store it on the class level, but the only reason I can see doing
so would be to determine if an operation is running.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:uR*************@TK2MSFTNGP05.phx.gbl...
I'm having a problem with my Thread usage and I think the general design
of how I'm working with them. My UI class calls a method in another
class that does a lot of work. That "worker" class looks something like
this(pseudo code):
class WorkerClass
{
Thread _listenerThread;

public WorkerClass()
{
_listenerThread = new Thread( new ThreadStart(ListenForInput) );
_listenerThread.IsBackground = true;
_listenerThread.Name = "Worker Listener Thread";
}

public void StartTest()
{
// do some quick stuff, then start the listener thread
_listenerThread.Start();
}

private void ListenForInput()
{
// make call to external library that
// won't return until a condition is met
}

public void CancelTest()
{
_listenerThread.Stop();
}
}
That is pretty close to what I have. So, I have a couple issues that I'm
dealing with. If I have run StartTest() once and call it again later (I
handle when it can be called, ListenForInput raises an event when it's
done) the _listenerThread's ThreadState is 'Background' so a subsequent
call to Start() throws an exception.

I figured I was having this behavior because the thread is a member of
the class rather than local to a method, but I need to have it as a
member so that I can cancel it from the UI if a user wishes.

I added some code to the end of ListenForInput() that would call Stop()
on the thread, but that seems dangerous to kill the thread you are
running on? In fact, is it safe for a process to modify ANYTHING about a
thread it's running on? Like priority or ThreadState?

Basically I'm just not sure how to design what I need. The above class
is an accurate model of what I'm after. Most of the threading examples I
have seen online or too simple and don't cover stuff like this.

Thanks for reading, I hope someone has some pointers for me.
-Steve


May 2 '06 #5

Steve wrote:
Hi Nicholas,

I agree that I should not create the thread in the ctor, I changed that
after reading your post. I also understand what you mean about the stop
flag being checked, it's like Jon Skeet's article that Larry posted.

My situation is maybe a bit unique in that my thread is stuck in a method
call that can't check for the stop flag. I would like to kill the thread
gracefully, but I'm not sure if I can.


Ah, so while waiting you are actually in third party code with no
timeout or way to neatly interrupt? Hmm. Well in that case, maybe hook
to ThreadInterruptedException, and do a Thread.Interrupt when you want
to signal the thread. As I understand it, Thread.Interrupt is much less
rude than Thread.Abort.

--
Larry Lard
Replies to group please

May 2 '06 #6
Steve,

I don't see why you can't check for the stop flag. Basically, instead
of checking the while loop for true, check the flag, like so:

// In class.
private boolean continueLoopLock = new object();
private boolean continueLoop;

private bool ContinueLoop
{
get
{
lock (continueLoopLock)
{
// Return the value.
return continueLoop;
}
}
set
{
lock (continueLoopLock)
{
// Set the value.
continueLoop = value;
}
}
}

// In the class.
Thread t = new Thread(new ThreadStart(LongMethod));
ContinueLoop = true;
t.Start();

private void LongMethod()
{
while (ContinueLoop)
{

}
}

Then, when you want it to stop, you just set ContinueLoop to false.

The property is there to make it easier to encapsulate the locking
logic, since you will have two threads accessing this at the same time.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:OS**************@TK2MSFTNGP05.phx.gbl...
Hi Nicholas,

I agree that I should not create the thread in the ctor, I changed that
after reading your post. I also understand what you mean about the stop
flag being checked, it's like Jon Skeet's article that Larry posted.

My situation is maybe a bit unique in that my thread is stuck in a method
call that can't check for the stop flag. I would like to kill the thread
gracefully, but I'm not sure if I can.

Basically my situation is:
Thread t = new Thread( new ThreadStart(LongMethod) );
t.Start();

private void LongMethod()
{
while(true)
{
//...
}
}
Any other suggestions?

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eK**************@TK2MSFTNGP02.phx.gbl...
Steve,

I think you are going about this the wrong way. You should rarely
call Stop on a thread in order to stop processing on it. Rather, you are
better off having some sort of shared variable which the thread will
check periodically to see if it should terminate. If the variable is
set, then the code in the thread will just exit, and the thread will die.

Also, you should create a new thread when you call StartTest, not when
you create your class. You should use a new thread each time. You can
choose to store it on the class level, but the only reason I can see
doing so would be to determine if an operation is running.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:uR*************@TK2MSFTNGP05.phx.gbl...
I'm having a problem with my Thread usage and I think the general design
of how I'm working with them. My UI class calls a method in another
class that does a lot of work. That "worker" class looks something like
this(pseudo code):
class WorkerClass
{
Thread _listenerThread;

public WorkerClass()
{
_listenerThread = new Thread( new ThreadStart(ListenForInput) );
_listenerThread.IsBackground = true;
_listenerThread.Name = "Worker Listener Thread";
}

public void StartTest()
{
// do some quick stuff, then start the listener thread
_listenerThread.Start();
}

private void ListenForInput()
{
// make call to external library that
// won't return until a condition is met
}

public void CancelTest()
{
_listenerThread.Stop();
}
}
That is pretty close to what I have. So, I have a couple issues that
I'm dealing with. If I have run StartTest() once and call it again
later (I handle when it can be called, ListenForInput raises an event
when it's done) the _listenerThread's ThreadState is 'Background' so a
subsequent call to Start() throws an exception.

I figured I was having this behavior because the thread is a member of
the class rather than local to a method, but I need to have it as a
member so that I can cancel it from the UI if a user wishes.

I added some code to the end of ListenForInput() that would call Stop()
on the thread, but that seems dangerous to kill the thread you are
running on? In fact, is it safe for a process to modify ANYTHING about a
thread it's running on? Like priority or ThreadState?

Basically I'm just not sure how to design what I need. The above class
is an accurate model of what I'm after. Most of the threading examples
I have seen online or too simple and don't cover stuff like this.

Thanks for reading, I hope someone has some pointers for me.
-Steve



May 2 '06 #7
My fault Nicholas, I should have been more specific. My LongMethod()
function isn't mine, I can't change it. It's a 3rd party library. I had
said that I can't check for the stop flag, but I didn't say why. Apologies.

-Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uo**************@TK2MSFTNGP05.phx.gbl...
Steve,

I don't see why you can't check for the stop flag. Basically, instead
of checking the while loop for true, check the flag, like so:

// In class.
private boolean continueLoopLock = new object();
private boolean continueLoop;

private bool ContinueLoop
{
get
{
lock (continueLoopLock)
{
// Return the value.
return continueLoop;
}
}
set
{
lock (continueLoopLock)
{
// Set the value.
continueLoop = value;
}
}
}

// In the class.
Thread t = new Thread(new ThreadStart(LongMethod));
ContinueLoop = true;
t.Start();

private void LongMethod()
{
while (ContinueLoop)
{

}
}

Then, when you want it to stop, you just set ContinueLoop to false.

The property is there to make it easier to encapsulate the locking
logic, since you will have two threads accessing this at the same time.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:OS**************@TK2MSFTNGP05.phx.gbl...
Hi Nicholas,

I agree that I should not create the thread in the ctor, I changed that
after reading your post. I also understand what you mean about the stop
flag being checked, it's like Jon Skeet's article that Larry posted.

My situation is maybe a bit unique in that my thread is stuck in a method
call that can't check for the stop flag. I would like to kill the thread
gracefully, but I'm not sure if I can.

Basically my situation is:
Thread t = new Thread( new ThreadStart(LongMethod) );
t.Start();

private void LongMethod()
{
while(true)
{
//...
}
}
Any other suggestions?

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eK**************@TK2MSFTNGP02.phx.gbl...
Steve,

I think you are going about this the wrong way. You should rarely
call Stop on a thread in order to stop processing on it. Rather, you
are better off having some sort of shared variable which the thread will
check periodically to see if it should terminate. If the variable is
set, then the code in the thread will just exit, and the thread will
die.

Also, you should create a new thread when you call StartTest, not
when you create your class. You should use a new thread each time. You
can choose to store it on the class level, but the only reason I can see
doing so would be to determine if an operation is running.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:uR*************@TK2MSFTNGP05.phx.gbl...
I'm having a problem with my Thread usage and I think the general
design of how I'm working with them. My UI class calls a method in
another class that does a lot of work. That "worker" class looks
something like this(pseudo code):
class WorkerClass
{
Thread _listenerThread;

public WorkerClass()
{
_listenerThread = new Thread( new ThreadStart(ListenForInput) );
_listenerThread.IsBackground = true;
_listenerThread.Name = "Worker Listener Thread";
}

public void StartTest()
{
// do some quick stuff, then start the listener thread
_listenerThread.Start();
}

private void ListenForInput()
{
// make call to external library that
// won't return until a condition is met
}

public void CancelTest()
{
_listenerThread.Stop();
}
}
That is pretty close to what I have. So, I have a couple issues that
I'm dealing with. If I have run StartTest() once and call it again
later (I handle when it can be called, ListenForInput raises an event
when it's done) the _listenerThread's ThreadState is 'Background' so a
subsequent call to Start() throws an exception.

I figured I was having this behavior because the thread is a member of
the class rather than local to a method, but I need to have it as a
member so that I can cancel it from the UI if a user wishes.

I added some code to the end of ListenForInput() that would call Stop()
on the thread, but that seems dangerous to kill the thread you are
running on? In fact, is it safe for a process to modify ANYTHING about
a thread it's running on? Like priority or ThreadState?

Basically I'm just not sure how to design what I need. The above class
is an accurate model of what I'm after. Most of the threading examples
I have seen online or too simple and don't cover stuff like this.

Thanks for reading, I hope someone has some pointers for me.
-Steve



May 2 '06 #8
Can you make the method fail or otherwise return some other way?

For example a traditional way to escape from thread blocked on a TCP read is
just to close the socket.

If you can make the 3rd party call return in some way you can use a state
variable to tell the thread that you did it on purpose.

"Steve" <sk**@skle.com> wrote in message
news:uf**************@TK2MSFTNGP02.phx.gbl...
My fault Nicholas, I should have been more specific. My LongMethod()
function isn't mine, I can't change it. It's a 3rd party library. I had
said that I can't check for the stop flag, but I didn't say why.
Apologies.

-Steve
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uo**************@TK2MSFTNGP05.phx.gbl...
Steve,

I don't see why you can't check for the stop flag. Basically, instead
of checking the while loop for true, check the flag, like so:

// In class.
private boolean continueLoopLock = new object();
private boolean continueLoop;

private bool ContinueLoop
{
get
{
lock (continueLoopLock)
{
// Return the value.
return continueLoop;
}
}
set
{
lock (continueLoopLock)
{
// Set the value.
continueLoop = value;
}
}
}

// In the class.
Thread t = new Thread(new ThreadStart(LongMethod));
ContinueLoop = true;
t.Start();

private void LongMethod()
{
while (ContinueLoop)
{

}
}

Then, when you want it to stop, you just set ContinueLoop to false.

The property is there to make it easier to encapsulate the locking
logic, since you will have two threads accessing this at the same time.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:OS**************@TK2MSFTNGP05.phx.gbl...
Hi Nicholas,

I agree that I should not create the thread in the ctor, I changed that
after reading your post. I also understand what you mean about the stop
flag being checked, it's like Jon Skeet's article that Larry posted.

My situation is maybe a bit unique in that my thread is stuck in a
method call that can't check for the stop flag. I would like to kill
the thread gracefully, but I'm not sure if I can.

Basically my situation is:
Thread t = new Thread( new ThreadStart(LongMethod) );
t.Start();

private void LongMethod()
{
while(true)
{
//...
}
}
Any other suggestions?

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:eK**************@TK2MSFTNGP02.phx.gbl...
Steve,

I think you are going about this the wrong way. You should rarely
call Stop on a thread in order to stop processing on it. Rather, you
are better off having some sort of shared variable which the thread
will check periodically to see if it should terminate. If the variable
is set, then the code in the thread will just exit, and the thread will
die.

Also, you should create a new thread when you call StartTest, not
when you create your class. You should use a new thread each time.
You can choose to store it on the class level, but the only reason I
can see doing so would be to determine if an operation is running.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Steve" <sk**@skle.com> wrote in message
news:uR*************@TK2MSFTNGP05.phx.gbl...
> I'm having a problem with my Thread usage and I think the general
> design of how I'm working with them. My UI class calls a method in
> another class that does a lot of work. That "worker" class looks
> something like this(pseudo code):
> class WorkerClass
> {
> Thread _listenerThread;
>
> public WorkerClass()
> {
> _listenerThread = new Thread( new
> ThreadStart(ListenForInput) );
> _listenerThread.IsBackground = true;
> _listenerThread.Name = "Worker Listener Thread";
> }
>
> public void StartTest()
> {
> // do some quick stuff, then start the listener thread
> _listenerThread.Start();
> }
>
> private void ListenForInput()
> {
> // make call to external library that
> // won't return until a condition is met
> }
>
> public void CancelTest()
> {
> _listenerThread.Stop();
> }
> }
>
>
> That is pretty close to what I have. So, I have a couple issues that
> I'm dealing with. If I have run StartTest() once and call it again
> later (I handle when it can be called, ListenForInput raises an event
> when it's done) the _listenerThread's ThreadState is 'Background' so a
> subsequent call to Start() throws an exception.
>
> I figured I was having this behavior because the thread is a member of
> the class rather than local to a method, but I need to have it as a
> member so that I can cancel it from the UI if a user wishes.
>
> I added some code to the end of ListenForInput() that would call
> Stop() on the thread, but that seems dangerous to kill the thread you
> are running on? In fact, is it safe for a process to modify ANYTHING
> about a thread it's running on? Like priority or ThreadState?
>
> Basically I'm just not sure how to design what I need. The above
> class is an accurate model of what I'm after. Most of the threading
> examples I have seen online or too simple and don't cover stuff like
> this.
>
> Thanks for reading, I hope someone has some pointers for me.
> -Steve
>



May 2 '06 #9
Let the third party fix their code, this is all you can do. Calling
Thread.Abort on a thread other than the currently executing thread is BAD,
you don't know what the the thread is actually doing, it's even possible
that he is blocked in unmanaged code, in which case a Thread.Abort won't
help you anyway.

Willy.
"Steve" <sk**@skle.com> wrote in message
news:uf**************@TK2MSFTNGP02.phx.gbl...
| My fault Nicholas, I should have been more specific. My LongMethod()
| function isn't mine, I can't change it. It's a 3rd party library. I had
| said that I can't check for the stop flag, but I didn't say why.
Apologies.
|
| -Steve
|
|
| "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
| message news:uo**************@TK2MSFTNGP05.phx.gbl...
| > Steve,
| >
| > I don't see why you can't check for the stop flag. Basically,
instead
| > of checking the while loop for true, check the flag, like so:
| >
| > // In class.
| > private boolean continueLoopLock = new object();
| > private boolean continueLoop;
| >
| > private bool ContinueLoop
| > {
| > get
| > {
| > lock (continueLoopLock)
| > {
| > // Return the value.
| > return continueLoop;
| > }
| > }
| > set
| > {
| > lock (continueLoopLock)
| > {
| > // Set the value.
| > continueLoop = value;
| > }
| > }
| > }
| >
| > // In the class.
| > Thread t = new Thread(new ThreadStart(LongMethod));
| > ContinueLoop = true;
| > t.Start();
| >
| > private void LongMethod()
| > {
| > while (ContinueLoop)
| > {
| >
| > }
| > }
| >
| > Then, when you want it to stop, you just set ContinueLoop to false.
| >
| > The property is there to make it easier to encapsulate the locking
| > logic, since you will have two threads accessing this at the same time.
| >
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Steve" <sk**@skle.com> wrote in message
| > news:OS**************@TK2MSFTNGP05.phx.gbl...
| >> Hi Nicholas,
| >>
| >> I agree that I should not create the thread in the ctor, I changed that
| >> after reading your post. I also understand what you mean about the
stop
| >> flag being checked, it's like Jon Skeet's article that Larry posted.
| >>
| >> My situation is maybe a bit unique in that my thread is stuck in a
method
| >> call that can't check for the stop flag. I would like to kill the
thread
| >> gracefully, but I'm not sure if I can.
| >>
| >> Basically my situation is:
| >> Thread t = new Thread( new ThreadStart(LongMethod) );
| >> t.Start();
| >>
| >> private void LongMethod()
| >> {
| >> while(true)
| >> {
| >> //...
| >> }
| >> }
| >>
| >>
| >> Any other suggestions?
| >>
| >> Thanks for the post!
| >> -Steve
| >>
| >>
| >>
| >>
| >> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
wrote
| >> in message news:eK**************@TK2MSFTNGP02.phx.gbl...
| >>> Steve,
| >>>
| >>> I think you are going about this the wrong way. You should rarely
| >>> call Stop on a thread in order to stop processing on it. Rather, you
| >>> are better off having some sort of shared variable which the thread
will
| >>> check periodically to see if it should terminate. If the variable is
| >>> set, then the code in the thread will just exit, and the thread will
| >>> die.
| >>>
| >>> Also, you should create a new thread when you call StartTest, not
| >>> when you create your class. You should use a new thread each time.
You
| >>> can choose to store it on the class level, but the only reason I can
see
| >>> doing so would be to determine if an operation is running.
| >>>
| >>> Hope this helps.
| >>>
| >>>
| >>> --
| >>> - Nicholas Paldino [.NET/C# MVP]
| >>> - mv*@spam.guard.caspershouse.com
| >>>
| >>> "Steve" <sk**@skle.com> wrote in message
| >>> news:uR*************@TK2MSFTNGP05.phx.gbl...
| >>>> I'm having a problem with my Thread usage and I think the general
| >>>> design of how I'm working with them. My UI class calls a method in
| >>>> another class that does a lot of work. That "worker" class looks
| >>>> something like this(pseudo code):
| >>>> class WorkerClass
| >>>> {
| >>>> Thread _listenerThread;
| >>>>
| >>>> public WorkerClass()
| >>>> {
| >>>> _listenerThread = new Thread( new
ThreadStart(ListenForInput) );
| >>>> _listenerThread.IsBackground = true;
| >>>> _listenerThread.Name = "Worker Listener Thread";
| >>>> }
| >>>>
| >>>> public void StartTest()
| >>>> {
| >>>> // do some quick stuff, then start the listener thread
| >>>> _listenerThread.Start();
| >>>> }
| >>>>
| >>>> private void ListenForInput()
| >>>> {
| >>>> // make call to external library that
| >>>> // won't return until a condition is met
| >>>> }
| >>>>
| >>>> public void CancelTest()
| >>>> {
| >>>> _listenerThread.Stop();
| >>>> }
| >>>> }
| >>>>
| >>>>
| >>>> That is pretty close to what I have. So, I have a couple issues that
| >>>> I'm dealing with. If I have run StartTest() once and call it again
| >>>> later (I handle when it can be called, ListenForInput raises an event
| >>>> when it's done) the _listenerThread's ThreadState is 'Background' so
a
| >>>> subsequent call to Start() throws an exception.
| >>>>
| >>>> I figured I was having this behavior because the thread is a member
of
| >>>> the class rather than local to a method, but I need to have it as a
| >>>> member so that I can cancel it from the UI if a user wishes.
| >>>>
| >>>> I added some code to the end of ListenForInput() that would call
Stop()
| >>>> on the thread, but that seems dangerous to kill the thread you are
| >>>> running on? In fact, is it safe for a process to modify ANYTHING
about
| >>>> a thread it's running on? Like priority or ThreadState?
| >>>>
| >>>> Basically I'm just not sure how to design what I need. The above
class
| >>>> is an accurate model of what I'm after. Most of the threading
examples
| >>>> I have seen online or too simple and don't cover stuff like this.
| >>>>
| >>>> Thanks for reading, I hope someone has some pointers for me.
| >>>> -Steve
| >>>>
| >>>
| >>>
| >>
| >>
| >
| >
|
|
May 2 '06 #10
_DD
On Tue, 2 May 2006 18:55:37 +0200, "Willy Denoyette [MVP]"
<wi*************@telenet.be> wrote:
Let the third party fix their code, this is all you can do. Calling
Thread.Abort on a thread other than the currently executing thread is BAD,
you don't know what the the thread is actually doing, it's even possible
that he is blocked in unmanaged code, in which case a Thread.Abort won't
help you anyway.


Here's something I was wondering about: If a file is opened in a
thread, and that fileopen is enclosed in a using {} or try/finally
block, does it get auto-closed if the thread is aborted?

I know that a hard return out of the block will still run the
finalizer, but it doesn't seem likely that abort would abide by the
same rules.
May 10 '06 #11

"_DD" <_D*@nospam.com> wrote in message
news:42********************************@4ax.com...
| On Tue, 2 May 2006 18:55:37 +0200, "Willy Denoyette [MVP]"
| <wi*************@telenet.be> wrote:
|
| >Let the third party fix their code, this is all you can do. Calling
| >Thread.Abort on a thread other than the currently executing thread is
BAD,
| >you don't know what the the thread is actually doing, it's even possible
| >that he is blocked in unmanaged code, in which case a Thread.Abort won't
| >help you anyway.
|
| Here's something I was wondering about: If a file is opened in a
| thread, and that fileopen is enclosed in a using {} or try/finally
| block, does it get auto-closed if the thread is aborted?
|

Yep, on V2 of the framework, it's guaranteed that'finally' will run
irrespective the kind of exception, of course if your thread blocks
indefinitely in unmanaged code, it won't get aborted so finally will never
run and your file will stay open until the process dies.
If the thread is aborted as a result of an ApplicationDomain unload, things
are a bit more complicated.

| I know that a hard return out of the block will still run the
| finalizer, but it doesn't seem likely that abort would abide by the
| same rules.

You mean 'finally' and NOT finalizer I suppose.

Willy.
May 10 '06 #12

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

Similar topics

0
by: David | last post by:
I've written a small windows service, and I'm having a problem that I'm spending a lot more time on than I'd like. If anyone has experienced this problem and has any hints or solutions; they would...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
1
by: Perre Van Wilrijk | last post by:
Hi, I've got a full text index which works fine, SQLSERVER2000/WIN 2000 SERVER. The system requires to update indexes immediately, so I use a timestamp field to enable this. No problems so...
5
by: Tuvas | last post by:
Is there a way to stop a thread with some command like t.stop()? Or any other neat way to get around it? Thanks!
2
by: Adam Barker | last post by:
Hi guys Not sure if my approach to threading in my app is causing more problems than it should. Let's say I have a form with four buttons on it. Each one goes away and does something...
3
by: Zeng | last post by:
Is the lock in these two places needed for multi-threading? I thought accessing a variable value should already be atomic. Thanks for your help or comments. bool stopping = false; public...
7
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web...
8
by: James A Taber | last post by:
What I want to implement is to restart certain thread every 24 hours... sample code: Dim T() As Thread Dim L() As Lib1.Class1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e...
2
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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...

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.