线程:停止,重启等 [英] Threading: Stopping, restarting, etc

查看:86
本文介绍了线程:停止,重启等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Thread使用有问题,我认为

的一般设计我是如何使用它们的。我的UI类在另一个类中调用一个方法,

做了很多工作。那个工人 class看起来像这样(伪

代码):

class WorkerClass

{

Thread _listenerThread;


public WorkerClass()

{

_listenerThread = new Thread(new ThreadStart(ListenForInput));

_listenerThread.IsBackground = true;

_listenerThread.Name =" Worker Listener Thread";

}


public void StartTest ()

{

//做一些快速的东西,然后启动监听器线程

_listenerThread.Start();

}


private void ListenForInput()

{

//调用外部库

//在满足条件之前不会返回

}


public void CancelTest()

{

_listenerThread.Stop();

}

}

这与我所拥有的非常接近。所以,我有几个问题,我要处理的是b $ b。如果我已经运行了一次StartTest()并稍后再次调用它(我可以调用
句柄,当它完成时,ListenForInput会引发一个事件)

_listenerThread'的ThreadState是''Background''所以后续调用

Start()会抛出异常。


我想我有这种行为因为该线程是

类的成员而不是方法的本地成员,但我需要将其作为成员使用

我可以从UI取消它如果用户愿意的话。


我在ListenForInput()的末尾添加了一些代码,它会在

线程上调用Stop(),但这似乎很危险杀死你正在运行的线程?

事实上,一个进程修改任何关于它运行的线程的安全性是否安全?喜欢优先级还是ThreadState?


基本上我只是不确定如何设计我需要的东西。上面的类是

我正在追求的准确模型。我在网上看到的大多数线程示例都是b
或者太简单而且不包括这样的东西。


感谢阅读,我希望有人有给我一些指示。

-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

推荐答案



史蒂夫写道:

Steve wrote:
我的Thread使用有问题,我认为
如何使用它们的一般设计。我的UI类在另一个类中调用一个方法,它可以完成很多工作。
[snip]基本上我只是不确定如何设计我需要的东西。上面的课程是我所追求的准确模型。我在网上看到的大多数线程示例或者过于简单,并且没有涵盖这样的内容。
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.




立即去阅读Jon Skeet'' s浏览页面

< http://yoda.arachsys.com/csharp/threads>,或者如果感觉很懒,只需要

''关闭工作线程优雅''页面

< http://yoda.arachsys.com/csharp/threads/shutdown.shtml> ;.


宝贵的资源!


-

Larry Lard

回复团体请求



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


史蒂夫,


我认为你这样做是错误的。您应该很少调用

停止某个线程以停止处理它。相反,你最好还是有一些共享变量,线程会定期查看
以确定它是否应该终止。如果设置了变量,则

线程中的代码将退出,并且线程将会死亡。


此外,您应该创建一个新线程当你打电话给StartTest时,而不是当你创建你的班级时。

。您应该每次都使用一个新线程。你可以选择将它存储在类级别上,但是我能看到这样做的唯一原因是确定操作是否正在运行。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com


" Steve" < SK ** @ skle.com>在消息中写道

news:uR ************* @ TK2MSFTNGP05.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...
我有一个我的线程使用问题,我认为我如何与他们合作的一般设计。我的UI类调用另一个类中的方法
,它做了很多工作。那个工人 class类似于
这个(伪代码):
类WorkerClass
{
线程_listenerThread;

公共WorkerClass()
{
_listenerThread = new Thread(new ThreadStart(ListenForInput));
_listenerThread.IsBackground = true;
_listenerThread.Name =" Worker Listener Thread" ;;
}
<公开void StartTest()
//做一些快速的东西,然后启动监听器线程
_listenerThread.Start();
}

private void ListenForInput()
//调用外部库
//在满足条件之前不会返回
}
<公共无效CancelTest()
{
_listenerThread.Stop();
}
}

这与我所拥有的非常接近。所以,我有几个我正在处理的问题。如果我已经运行了一次StartTest()并稍后再次调用它(我可以在可以调用它时处理它,当它完成时,ListenForInput会引发一个事件)_listenerThread'的ThreadState是''后台''所以随后对Start()的调用会引发异常。

我认为我有这种行为,因为该线程是
类的成员而不是本地的一个方法,但我需要把它作为一个成员,所以如果用户愿意我可以从UI取消它。

我在ListenForInput()的末尾添加了一些代码这会在线程上调用Stop(),但是杀死你正在运行的线程似乎很危险?事实上,一个进程修改任何关于它正在运行的线程的安全性是否安全?喜欢优先级还是ThreadState?

基本上我只是不确定如何设计我需要的东西。上面的课程是我所追求的准确模型。我在网上看到的大多数线程示例都太简单了,也没有涵盖这样的内容。

感谢您的阅读,我希望有人能为我提供一些指示。 -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



很酷,我在google搜索时找到了第一个链接,但是关于

停止的第二个链接很有意思。乍一看,它似乎是最好的b / b
适合迭代过程,有机会检查

停止标志。我的线程调用一个函数,直到

满足特定条件才会返回。所以从理论上讲,它可以坐在那里等待
天。


我不满意它以这种方式操作或不是''有一个

超时参数,但它不是我的库而且我不能改变它。


John的文章给了我更多的理解,但我仍然没有看到如何前进的明确路径。我会继续阅读更多内容和

试验。


感谢您的帖子,

Steve

Larry Lard < LA ******* @ hotmail.com>在消息中写道

news:11 ******************** @ y43g2000cwc.googlegrou ps.com ...
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:
我的线程使用有问题,我认为
我是如何使用的一般设计
他们。我的UI类调用另一个类中的方法

做了很多工作。
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]


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



立即去阅读Jon Skeet'' s浏览页面
< http://yoda.arachsys.com/csharp/threads>,或者如果感觉很懒,只需点击
''关闭工作线程优雅'页面< http://yoda.arachsys.com/csharp/threads/shutdown.shtml>。

宝贵的资源!

-
拉里拉德
回复团体请求



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



这篇关于线程:停止,重启等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆