S
Steve
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
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