By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

Thread Strange Behaviour

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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...
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

Nov 17 '05 #2

P: n/a
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();

Nov 17 '05 #3

P: n/a
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...
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();

Nov 17 '05 #4

P: n/a
Further to what Nick has said,

http://msdn.microsoft.com/library/de...spendTopic.asp

read the 'caution' near the bottom of the page

If you want a sample check out Jon Skeet's pages:

http://www.yoda.arachsys.com/csharp/threads/

HTH

Ollie Riches

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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();

Nov 17 '05 #5

P: n/a
I read the part recommended. So whats the real use of suspend,
resume,etc methods?

Also, how can I get around the problem, to have the behaviour of
starting, stopping, pausing and resuming from a thread.

Nov 17 '05 #6

P: n/a
Curious <th****@mail.global.net.mt> wrote:
I read the part recommended. So whats the real use of suspend,
resume,etc methods?
Very little, IMO.
Also, how can I get around the problem, to have the behaviour of
starting, stopping, pausing and resuming from a thread.


You should use Monitor.Wait/Pulse/PulseAll or WaitHandles (such as
AutoResetEvent).

See http://www.pobox.com/~skeet/csharp/threads

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

P: n/a
Is the following code safe??

protected void Execute()
{
while (Running == true)
{
[code]

if (Paused == true)
{
try
{
Thread.Sleep(Timeout.Infinite);
}
catch (ThreadInterruptedException){}
}
}
}

Thread t1 = new Thread(new ThreadStart(Execute));
Running = true;
Paused = false;
t1.Start();

Console.Writeline("Press any key to Pause");
Console.Readline();
Paused = true;

Console.Writline("Press any key to Resume");
Console.Readline();
Paused = true;
t1.Interrupt();

Console.Writline("Press any key to Stop");
Console.Readline();
Running = false;

Nov 17 '05 #8

P: n/a
Curious wrote:
Is the following code safe??


<snip>

Well:

1) It depends on how Running and Paused are implemented (assuming
they're properties)
2) Even if it's thread-safe, it's not the way you're *meant* to pause
and resume threads. As I said before, you should use monitors or
waithandles - that's what they're *designed* for.

Jon

Nov 17 '05 #9

P: n/a
how about the following example console app - I am sure Jon will point out
some subtle problem with the example :)

using System;
using System.Threading;

namespace TestThread
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private ManualResetEvent _pauseEvent = new ManualResetEvent(false);
private ManualResetEvent _stopEvent = new ManualResetEvent(false);
private ManualResetEvent _finishedEvent = new
ManualResetEvent(false);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 c1 = new Class1();
// Pause the worker
c1._pauseEvent.Set();
Thread t1 = new Thread(new ThreadStart(c1.Worker));
t1.Start();
Console.WriteLine("Press ENTER to start worker");
Console.ReadLine();
// Start the worker
c1._pauseEvent.Reset();
Console.WriteLine("Press ENTER to pause worker");
Console.ReadLine();
// Pause worker
c1._pauseEvent.Set();
Console.WriteLine("Press ENTER to stop worker");
Console.ReadLine();
// Stop worker
c1._stopEvent.Set();
c1._pauseEvent.Reset();
c1._finishedEvent.WaitOne();
Console.WriteLine("Worker Stopped");
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Press ENTER to exit");
Console.ReadLine();
}
private void Worker()
{
System.Diagnostics.Trace.WriteLine("Starting worker");
int i = 0;
// Check whether the worker is to shut down (waits for 100th of
second)
while(this._stopEvent.WaitOne(10, false) == false)
{
// Check whether the worker has been paused (waits for 100th
of second)
while(this._pauseEvent.WaitOne(10, false) == false)
{
i++;
System.Diagnostics.Trace.WriteLine("Iteration = " +
i + ", Date = " + DateTime.Now.ToString("U"));
}
}
System.Diagnostics.Trace.WriteLine("Shutting down worker");
this._finishedEvent.Set();
}
}
}

HTH

Ollie Riches

"Curious" <th****@mail.global.net.mt> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Is the following code safe??

protected void Execute()
{
while (Running == true)
{
[code]

if (Paused == true)
{
try
{
Thread.Sleep(Timeout.Infinite);
}
catch (ThreadInterruptedException){}
}
}
}

Thread t1 = new Thread(new ThreadStart(Execute));
Running = true;
Paused = false;
t1.Start();

Console.Writeline("Press any key to Pause");
Console.Readline();
Paused = true;

Console.Writline("Press any key to Resume");
Console.Readline();
Paused = true;
t1.Interrupt();

Console.Writline("Press any key to Stop");
Console.Readline();
Running = false;

Nov 17 '05 #10

P: n/a
Ollie Riches wrote:
how about the following example console app - I am sure Jon will point out
some subtle problem with the example :)


Well, it's somewhat inefficient to wake up every 10ms (and personally I
don't like comparisons against boolean literals, but that's somewhat
different :) - I'd use WaitAny instead, and see what was signalled,
*or* use a single monitor and two flags (or even an enum) for
paused/terminated.

Having said that, I don't think it was unthreadsafe in any way :)

Jon

Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.