Un-tighten tight loop

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Dear All,

I have a section of code that is something along the lines of:

private void threadFunc ()
{
while (true)
{
object o = ourOwnQueueObject.GetObject (); // Non blocking.
processObject (o); // Again nothing blocks in this function.

Thread.Sleep (1); // Untighten loop.
}
}

This function is spawned at app start. Abort () is called on this
thread on app close. If, however, abort is called whilst the thread
is sleeping I get an exception (as has been well discussed here and
elsewhere).

I could replace the line 'while (true)' with something like 'while
(keepGoing)' and change keepGoing to false on app close.

Does anybody know any other way to untighten the loop?

Discussion/debate/ideas welcome.

Thanks in advance,

Nick
 
Thread.Abort may not be your best option to stop the thread. This will
generate a ThreadAbortedException on that thread and may lead to invalid
states in your objects as the codepath is broken.

You could try something like this:

using System;
using System.Threading;

class Program
{
static System.Threading.AutoResetEvent stopEvent = new
AutoResetEvent(false);

static void Main(string[] args)
{
Thread wt = new Thread(new ThreadStart(threadFunc));
wt.Start();

Console.WriteLine("Press enter to stop thread");
Console.ReadLine();
stopEvent.Set();
wt.Join();

Console.WriteLine("Thread stopped");
Console.ReadLine();
}

static void threadFunc ()
{
while(stopEvent.WaitOne(new TimeSpan(0, 0, 1), false) == false)
{
// object o = ourOwnQueueObject.GetObject (); // Non blocking.
// processObject (o); // Again nothing blocks in this function.
Console.WriteLine("Thread working...");
}
}
}

Regards,
Joakim
 
Nick said:
Dear All,

I have a section of code that is something along the lines of:

private void threadFunc ()
{
while (true)
{
object o = ourOwnQueueObject.GetObject (); // Non blocking.
processObject (o); // Again nothing blocks in this function.

Thread.Sleep (1); // Untighten loop.
}
}

This function is spawned at app start. Abort () is called on this
thread on app close. If, however, abort is called whilst the thread
is sleeping I get an exception (as has been well discussed here and
elsewhere).

I could replace the line 'while (true)' with something like 'while
(keepGoing)' and change keepGoing to false on app close.

Does anybody know any other way to untighten the loop?

Discussion/debate/ideas welcome.

Thanks in advance,

Nick

Hi,

isn't it possible to make the GetObject blocking ? That case, you can
remove the call to Thread.Sleep. It looks like a typical
Producer-Consumer problem to me. You will need events and mutexes to
accomplish this. I don't know if exceptions are also thrown when a
tread is aborted while waiting on an event.

BTW, if the exception happens while the thread is asleep, nothing can go
severely wrong (I guess). So you could silently catch the exception
outside the loop ? I guess you only want to mask the exception ?

Cheers,
Benoit
 
If you call Abort on a thread an exception is always thrown whether the thread is blocked or not - thats why its a really bad idea in most situations. The boolean flag is a cleanerr way of bringing down the thread

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

isn't it possible to make the GetObject blocking ? That case, you can
remove the call to Thread.Sleep. It looks like a typical
Producer-Consumer problem to me. You will need events and mutexes to
accomplish this. I don't know if exceptions are also thrown when a
tread is aborted while waiting on an event.

BTW, if the exception happens while the thread is asleep, nothing can go
severely wrong (I guess). So you could silently catch the exception
outside the loop ? I guess you only want to mask the exception ?

Cheers,
Benoit
 
Back
Top