线程问题。 [英] Threading problem.

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

问题描述

大家好,


当我第一次在我的

代码上调用Start()线程时,我遇到了一些线程问题工作,我调用Stop()并停止,但当我再次调用Start()

时,我的线程没有启动。虽然在调试器中我可以看到它

转到Start()方法并再次创建线程。


任何人都可以帮助我吗?这是代码:


< snip>


public static MySingleton GetInstance(){

if( instance == null){

instance = new MySingleton();

}

返回实例;

}


public void Start(){

lock(this){

if(!started){

started = true;

thread = new Thread(new ThreadStart(Run));

thread.Start();

}

}

}


public void Stop(){

stopped = true;

}


private void Run(){

while(!stopped){

Console.WriteLine (正在运行......);

Thread.Sleep(1000);

}

thread.Join();

开始=假;

}


< / snip>

谢谢,


/ m

Hi All,

I''m having a bit of problem with threading, when I first call Start() on my
code the thread work, I call Stop() and it stopped, but when I call Start()
again my thread is not started. Although in the debugger I can see that it
went to the Start() method and creating the thread again.

Can anyone help me? This is the code:

<snip>

public static MySingleton GetInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}

public void Start() {
lock (this) {
if (!started){
started = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}

public void Stop() {
stopped = true;
}

private void Run() {
while (!stopped) {
Console.WriteLine("Running ...");
Thread.Sleep(1000);
}
thread.Join();
started = false;
}

</snip>
Thanks,

/m

推荐答案

在Stop方法上尝试thread.Abort()


希望这有助于

Dan Cimpoiesu


" muscha" <亩**** @ no.spam.net>在消息中写道

news:eh ************* @ tk2msftngp13.phx.gbl ...
Try thread.Abort() on the Stop method

Hope this helps
Dan Cimpoiesu

"muscha" <mu****@no.spam.net> wrote in message
news:eh*************@tk2msftngp13.phx.gbl...
大家好,
我遇到线程问题,当我第一次在
上调用Start()我的代码线程工作时,我调用Stop()然后停止了,但是当我调用$ b时$ b再次启动()我的线程未启动。虽然在调试器中我可以看到它转到Start()方法并再次创建线程。

任何人都可以帮助我吗?这是代码:

< snip>

public static MySingleton GetInstance(){
if(instance == null){
instance =新的MySingleton();
}
返回实例;
}
公共无效Start(){
锁定(此){
如果(!开始){
start = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}

public void Stop(){
stopped = true;
}
私有无效Run(){
while (!停止){
Console.WriteLine(" Running ...");
Thread.Sleep(1000);
}
thread.Join();
启动=假;
}
< / snip>

谢谢,

/ m
Hi All,

I''m having a bit of problem with threading, when I first call Start() on my code the thread work, I call Stop() and it stopped, but when I call Start() again my thread is not started. Although in the debugger I can see that it
went to the Start() method and creating the thread again.

Can anyone help me? This is the code:

<snip>

public static MySingleton GetInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}

public void Start() {
lock (this) {
if (!started){
started = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}

public void Stop() {
stopped = true;
}

private void Run() {
while (!stopped) {
Console.WriteLine("Running ...");
Thread.Sleep(1000);
}
thread.Join();
started = false;
}

</snip>
Thanks,

/m



muscha< mu **** @ no.spam.net>写道:
muscha <mu****@no.spam.net> wrote:
我遇到线程问题,当我第一次调用我的
代码上的Start()线程工作时,我调用Stop()并停止了,但是当我再次调用Start()
时,我的线程没有启动。虽然在调试器中我可以看到它转到Start()方法并再次创建线程。
I''m having a bit of problem with threading, when I first call Start() on my
code the thread work, I call Stop() and it stopped, but when I call Start()
again my thread is not started. Although in the debugger I can see that it
went to the Start() method and creating the thread again.




你的代码有点奇怪 - 我不喜欢不知道为什么你要在Run中调用

thread.Join(),当它运行的线程应该是

线程时无论如何都试图加入!


另请注意,您的单例实现并非线程安全 - 请参阅
http://www.pobox.com/~skeet/csharp/singleton.html


最后,你正在测试停止的方式。旗帜是狡猾的 - 因为

你还没有任何同步,不能保证

其他线程会看到新值。


如果您可以发布一个简短的*完整*示例来演示

问题,那会有所帮助。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Your code is slightly odd - I don''t see why you''re trying to call
thread.Join() within Run, when the thread it''s running in should be the
thread it''s trying to join anyway!

Note also that your singleton implementation isn''t threadsafe - see
http://www.pobox.com/~skeet/csharp/singleton.html

Finally, the way you''re testing the "stopped" flag is dodgy - because
you haven''t got any synchronization there, there''s no guarantee that
other threads will see new values.

If you could post a short but *complete* example which demonstrates the
problem, that would help.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


更改以下两种方法,它应该有效:


public void Stop()

{

lock (这)

{

停止=真;

thread.Join();

started = false;

停止=假;

}

}


private void Run()

{

while(!停止)

{

Console.WriteLine(" Running ...");

Thread.Sleep(10 00);

}

}

$ b $bJosé

" muscha" <亩**** @ no.spam.net>在消息中写道

news:eh ************* @ tk2msftngp13.phx.gbl ...
Change the 2 methods below and it should work:

public void Stop()
{
lock (this)
{
stopped = true;
thread.Join();
started = false;
stopped = false;
}
}

private void Run()
{
while (!stopped)
{
Console.WriteLine("Running ...");
Thread.Sleep(1000);
}
}

José
"muscha" <mu****@no.spam.net> wrote in message
news:eh*************@tk2msftngp13.phx.gbl...
大家好,
我遇到线程问题,当我第一次在
上调用Start()我的代码线程工作时,我调用Stop()然后停止了,但是当我调用$ b时$ b再次启动()我的线程未启动。虽然在调试器中我可以看到它转到Start()方法并再次创建线程。

任何人都可以帮助我吗?这是代码:

< snip>

public static MySingleton GetInstance(){
if(instance == null){
instance =新的MySingleton();
}
返回实例;
}
公共无效Start(){
锁定(此){
如果(!开始){
start = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}

public void Stop(){
stopped = true;
}
私有无效Run(){
while (!停止){
Console.WriteLine(" Running ...");
Thread.Sleep(1000);
}
thread.Join();
启动=假;
}
< / snip>

谢谢,

/ m
Hi All,

I''m having a bit of problem with threading, when I first call Start() on my code the thread work, I call Stop() and it stopped, but when I call Start() again my thread is not started. Although in the debugger I can see that it
went to the Start() method and creating the thread again.

Can anyone help me? This is the code:

<snip>

public static MySingleton GetInstance() {
if (instance == null) {
instance = new MySingleton();
}
return instance;
}

public void Start() {
lock (this) {
if (!started){
started = true;
thread = new Thread(new ThreadStart(Run));
thread.Start();
}
}
}

public void Stop() {
stopped = true;
}

private void Run() {
while (!stopped) {
Console.WriteLine("Running ...");
Thread.Sleep(1000);
}
thread.Join();
started = false;
}

</snip>
Thanks,

/m



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

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