线程奇怪的行为 [英] Thread Strange Behaviour

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

问题描述




我有一个工作线程在运行,基本上它的操作是在无限循环中执行



现在,当我暂停此线程时,CPU使用时间仍为

剩余100%。


我的问题可能是什么? 。

线程t1 =新线程(新的ThreadStart(myMethod));

Console.Writeline(按任意键开始);

Console.Readline();

running = true;

t1.Start();

Console.Writeline("按任意暂停);

Console.Readline();

t1.Suspend();

Console.Readline();

...这里的CPU使用率仍为100%

myMethod()

{

while(运行) == true)

{

[code]

}

}

有人可以帮我吗

提前致谢

Hi,

I have a worker thread running, where basically its operation is to
execute in an infinite loop.

Now, when I am suspending this thread, the CPU usage time is still
remaining 100%.

What could my problem be.
Thread t1 = new Thread(new ThreadStart(myMethod));
Console.Writeline("Press any key to start");
Console.Readline();
running = true;
t1.Start();
Console.Writeline("Press any key to suspend");
Console.Readline();
t1.Suspend();
Console.Readline();
... here CPU usage still 100%

myMethod()
{
while (running == true)
{
[code]
}
}
Can someone help me out
Thanks in Advance

推荐答案

好奇,

你不应该'' t在线程上使用Suspend方法来同步工作。

你应该使用事件或互斥或其他同步

原语来做到这一点。


调用挂起的目的是什么?

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Curious" <第**** @ mail.global.net.mt>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
Curious,

You shouldn''t use the Suspend method on a thread to synchronize work.
You should be using events or mutexes or some other synchronization
primitive to do this.

What is the purpose of calling Suspend?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...


我有一个工作线程在运行,基本上它的操作是在无限循环中执行。

现在,当我暂停这个线程,CPU使用时间仍然是100%。

我的问题是什么。

线程t1 =新线程(新的ThreadStart(myMethod) ));
Console.Writeline(按任意键开始);
Console.Readline();
running = true;
t1.Start(); <控制台.Writeline(按任意键暂停);
Console.Readline();
t1.Suspend();
Console.Readline();
..这里的CPU使用率仍为100%

myMethod()
{
while(running == true)
{
[code]
}
}

有人可以帮我吗
先谢谢
Hi,

I have a worker thread running, where basically its operation is to
execute in an infinite loop.

Now, when I am suspending this thread, the CPU usage time is still
remaining 100%.

What could my problem be.
Thread t1 = new Thread(new ThreadStart(myMethod));
Console.Writeline("Press any key to start");
Console.Readline();
running = true;
t1.Start();
Console.Writeline("Press any key to suspend");
Console.Readline();
t1.Suspend();
Console.Readline();
.. here CPU usage still 100%

myMethod()
{
while (running == true)
{
[code]
}
}
Can someone help me out
Thanks in Advance



我正在使用暂停以暂停执行。


为了解决以前的问题,我做了如下(这是正确的方法):


object objLock = new object();


myMethod()

{

while(running == true)

{

锁定(objLock)

{

[code]

}

}

}

线程t1 =新线程(新的ThreadStart(myMethod));

Console.Writeline(按任意键开始);

Console.Readline();

running = true;

t1.Start();


Console.Writeline(按任意键暂停);

Console.Readline();

lock(objLock)

{

t1.Suspend();

}

Console.Readline();


控制台.Writeline(按任意键恢复);

Console.Readline();

t1.Resume();

I am using the Suspend to pause the execution.

To solve previous problem, I did as follows (Is this the correct way):

object objLock = new object();

myMethod()
{
while (running == true)
{
lock(objLock)
{
[code]
}
}
}
Thread t1 = new Thread(new ThreadStart(myMethod));
Console.Writeline("Press any key to start");
Console.Readline();
running = true;
t1.Start();

Console.Writeline("Press any key to suspend");
Console.Readline();
lock(objLock)
{
t1.Suspend();
}
Console.Readline();

Console.Writeline("Press any key to resume");
Console.Readline();
t1.Resume();


好奇,


不,你没有做任何事情并导致非常麻烦

情况。


如果有的话,创建一个事件,然后等待该事件句柄。这将导致执行暂停,具体取决于你放置它的位置。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


好奇 <第**** @ mail.global.net.mt>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
Curious,

No, you aren''t doing anything really and causing a really messy
situation.

If anything, create an event, and wait on that event handle. This will
cause execution to pause, depending on where you put it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
我正在使用Suspend暂停执行。

为了解决以前的问题,我做了如下(这是正确的方法):

对象objLock =新对象();

myMethod()
{
while(running == true)
{
lock(objLock)
{
[code]
}
}
}
线程t1 =新线程(新的ThreadStart(myMethod));
Console.Writeline (按任意键开始);
Console.Readline();
running = true;
t1.Start();

Console.Writeline (按任意键暂停);
Console.Readline();
lock(objLock)
{
t1.Suspend();
} < /> Console.Readline();

Console.Writeline(按任意键恢复);
Console.Readline();
t1.Resume() ;
I am using the Suspend to pause the execution.

To solve previous problem, I did as follows (Is this the correct way):

object objLock = new object();

myMethod()
{
while (running == true)
{
lock(objLock)
{
[code]
}
}
}
Thread t1 = new Thread(new ThreadStart(myMethod));
Console.Writeline("Press any key to start");
Console.Readline();
running = true;
t1.Start();

Console.Writeline("Press any key to suspend");
Console.Readline();
lock(objLock)
{
t1.Suspend();
}
Console.Readline();

Console.Writeline("Press any key to resume");
Console.Readline();
t1.Resume();



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

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