473,761 Members | 10,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Lis tenForInput) );
_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 1871

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.c om/csharp/threads>, or if feeling lazy, just the
'Shutting down worker threads gracefully' page at
<http://yoda.arachsys.c om/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.co m

"Steve" <sk**@skle.co m> wrote in message
news:uR******** *****@TK2MSFTNG P05.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(Lis tenForInput) );
_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*******@hotm ail.com> wrote in message
news:11******** ************@y4 3g2000cwc.googl egroups.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.c om/csharp/threads>, or if feeling lazy, just the
'Shutting down worker threads gracefully' page at
<http://yoda.arachsys.c om/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(Lon gMethod) );
t.Start();

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

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eK******** ******@TK2MSFTN GP02.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.co m

"Steve" <sk**@skle.co m> wrote in message
news:uR******** *****@TK2MSFTNG P05.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(Lis tenForInput) );
_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 ThreadInterrupt edException, and do a Thread.Interrup t when you want
to signal the thread. As I understand it, Thread.Interrup t 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 continueLoopLoc k = new object();
private boolean continueLoop;

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

// In the class.
Thread t = new Thread(new ThreadStart(Lon gMethod));
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.co m

"Steve" <sk**@skle.co m> wrote in message
news:OS******** ******@TK2MSFTN GP05.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(Lon gMethod) );
t.Start();

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

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eK******** ******@TK2MSFTN GP02.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.co m

"Steve" <sk**@skle.co m> wrote in message
news:uR******** *****@TK2MSFTNG P05.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(Lis tenForInput) );
_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.c om> wrote in
message news:uo******** ******@TK2MSFTN GP05.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 continueLoopLoc k = new object();
private boolean continueLoop;

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

// In the class.
Thread t = new Thread(new ThreadStart(Lon gMethod));
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.co m

"Steve" <sk**@skle.co m> wrote in message
news:OS******** ******@TK2MSFTN GP05.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(Lon gMethod) );
t.Start();

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

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eK******** ******@TK2MSFTN GP02.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.co m

"Steve" <sk**@skle.co m> wrote in message
news:uR******** *****@TK2MSFTNG P05.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(Lis tenForInput) );
_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.co m> wrote in message
news:uf******** ******@TK2MSFTN GP02.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.c om> wrote
in message news:uo******** ******@TK2MSFTN GP05.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 continueLoopLoc k = new object();
private boolean continueLoop;

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

// In the class.
Thread t = new Thread(new ThreadStart(Lon gMethod));
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.co m

"Steve" <sk**@skle.co m> wrote in message
news:OS******** ******@TK2MSFTN GP05.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(Lon gMethod) );
t.Start();

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

Thanks for the post!
-Steve


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eK******** ******@TK2MSFTN GP02.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.co m

"Steve" <sk**@skle.co m> wrote in message
news:uR******** *****@TK2MSFTNG P05.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(Lis tenForInput) );
> _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.co m> wrote in message
news:uf******** ******@TK2MSFTN GP02.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.c om> wrote
in
| message news:uo******** ******@TK2MSFTN GP05.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 continueLoopLoc k = new object();
| > private boolean continueLoop;
| >
| > private bool ContinueLoop
| > {
| > get
| > {
| > lock (continueLoopLo ck)
| > {
| > // Return the value.
| > return continueLoop;
| > }
| > }
| > set
| > {
| > lock (continueLoopLo ck)
| > {
| > // Set the value.
| > continueLoop = value;
| > }
| > }
| > }
| >
| > // In the class.
| > Thread t = new Thread(new ThreadStart(Lon gMethod));
| > 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.co m
| >
| > "Steve" <sk**@skle.co m> wrote in message
| > news:OS******** ******@TK2MSFTN GP05.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(Lon gMethod) );
| >> t.Start();
| >>
| >> private void LongMethod()
| >> {
| >> while(true)
| >> {
| >> //...
| >> }
| >> }
| >>
| >>
| >> Any other suggestions?
| >>
| >> Thanks for the post!
| >> -Steve
| >>
| >>
| >>
| >>
| >> "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om>
wrote
| >> in message news:eK******** ******@TK2MSFTN GP02.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.co m
| >>>
| >>> "Steve" <sk**@skle.co m> wrote in message
| >>> news:uR******** *****@TK2MSFTNG P05.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(Lis tenForInput) );
| >>>> _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

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

Similar topics

0
3502
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 be appreciated. Simply leaving the threadMain() method if something unexpected occurs during thread execution leaves the thread in a ThreadState.Running state. This (apparently??) causes the Thread.Join() in service.OnStop() to hang...
77
5381
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
1
5189
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 far. Now, I've got a stored procedures which nearly daily inserts about 10.000 rows. When doing this while full text indexing is active, all users start complaining about performance. In order to work around this problem I tried
5
8560
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
283
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 extensive (data retrieval from the web) and displays a result on the form. Whats the best way to code this, bearing in mind that if you click another
3
2873
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 bool Stopping { get {
7
10077
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 service but there is no return from the service. Stopping and restarting the Windows service cures the problem. Our desire is to cure the problem with appropriate error handling but failing that, is there an easy way to automatically stop and...
8
1042
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 As System.EventArgs) Handles Button1.Click Dim i As Integer = 0
2
1537
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 synchronization objects instead and oh by the way you shouldn't be using them as they can cause deadlock and other major problems, screams bug or threading architecture issue and lack of willingness or priority to correct. In reality those API's are for...
0
9531
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
10115
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...
0
9957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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,...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5229
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...
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
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.