Thread question

J

[Joe]

Hi,
I have 3 questions , remark as Why ??? :

I have 2 threads that run in my application, each thread is a loop with low
level call :

While(!AutoSetEvent.WaitOne(1,true))
{
DLL_Export_GetData(…)
Do something with this data
}

Know some time I want to Abort those threads , So what I did is to create
AutoSetEvent , when I want to stop the thread I call the Set event function.
Then wait in loop until the thread died :
While(thread.IsAlive())
{
Continue;
}
In the last loop I tried to add Application.DoEvent() but this cause my
application to crash. Why ???

The big problem is sometime the thread is go out of the loop but it is still
Alive Why?????

Do any one know how to do that in deferent and elegant way ????
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

I wouldn't bother with the AutoSetEvent to determine if your loop should
still process. Rather, I would just say to have a boolean value which
access is synchronized to, and then check that in your loop.

As far as calling DoEvents in your loop which checks the thread if it is
alive, why are you doing that? Are you in a UI thread? Also, you really
shouldn't do this, IMO. DoEvents is just poor design.

The ^ideal^ situation, IMO, is to have a delegate that the thread has
access to which is called when the thread is done, and you continue your
work from that delegate. Otherwise, if you have to block another thread to
wait for your worker thread to finish, I would have some sort of Event that
the worker thread calls when the loop ends, and then wait on that in the
controlling thread.
 
P

Peter Duniho

Joe,

I wouldn't bother with the AutoSetEvent to determine if your loop
should
still process. Rather, I would just say to have a boolean value which
access is synchronized to, and then check that in your loop.

And in this case, since the variable would be read-only in the threads
using them to decide whether to exit, and written only by some controlling
thread, synchronization can be as simple as just making the variable
"volatile".
As far as calling DoEvents in your loop which checks the thread if
it is
alive, why are you doing that? Are you in a UI thread? Also, you really
shouldn't do this, IMO. DoEvents is just poor design.

Agreed a thousand times over. :)
The ^ideal^ situation, IMO, is to have a delegate that the thread has
access to which is called when the thread is done, and you continue your
work from that delegate. Otherwise, if you have to block another thread
to
wait for your worker thread to finish, I would have some sort of Event
that
the worker thread calls when the loop ends, and then wait on that in the
controlling thread.

But, I hope that Nicholas's main point -- that having a thread simply
sitting and waiting for some other thread(s) to complete is not a good
idea -- comes across here.

Yes, you could do it. But there's much point in it. There are better
ways to deal with that sort of thing.

Pete
 
J

[Joe]

Thanks a lot, One more quistion for my general knolage, What Thread.Abort()
do, I never saw this call cause anything :-( .

Nicholas Paldino said:
Joe,

I wouldn't bother with the AutoSetEvent to determine if your loop should
still process. Rather, I would just say to have a boolean value which
access is synchronized to, and then check that in your loop.

As far as calling DoEvents in your loop which checks the thread if it is
alive, why are you doing that? Are you in a UI thread? Also, you really
shouldn't do this, IMO. DoEvents is just poor design.

The ^ideal^ situation, IMO, is to have a delegate that the thread has
access to which is called when the thread is done, and you continue your
work from that delegate. Otherwise, if you have to block another thread to
wait for your worker thread to finish, I would have some sort of Event that
the worker thread calls when the loop ends, and then wait on that in the
controlling thread.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I have 3 questions , remark as Why ??? :

I have 2 threads that run in my application, each thread is a loop with
low
level call :

While(!AutoSetEvent.WaitOne(1,true))
{
DLL_Export_GetData(.)
Do something with this data
}

Know some time I want to Abort those threads , So what I did is to create
AutoSetEvent , when I want to stop the thread I call the Set event
function.
Then wait in loop until the thread died :
While(thread.IsAlive())
{
Continue;
}
In the last loop I tried to add Application.DoEvent() but this cause my
application to crash. Why ???

The big problem is sometime the thread is go out of the loop but it is
still
Alive Why?????

Do any one know how to do that in deferent and elegant way ????
 
P

Peter Duniho

Thanks a lot, One more quistion for my general knolage, What
Thread.Abort() do, I never saw this call cause anything :-( .

It won't do anything when the thread is executing unmanaged code. It
_should_ cause a ThreadAbortException to be raised when the thread is
executing managed code (including as soon as it returns from a call to
unmanaged code). What happens at that point depends on what else is in
the managed code. Unlike the unmanaged thread aborting techniques,
calling Thread.Abort() may or may not actually terminate the thread,
depending on what else is done when the exception is thrown.

But at the very least, the exception should be thrown, eventually. If
it's left unhandled, the managed thread should in fact terminate (or if a
thread pool thread, stop executing the assigned task and be returned to
the thread pool).

All of that information should be purely academic. There's really no
place for calling Thread.Abort() in code that's well-designed, IMHO.

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

Well, the Abort method is meant to do what it says, abort the processing
on the thread you call it on. However, using that to stop your thread from
processing is a bad idea. You should be sending a specific signal to the
thread (whether it be a synchronized variable change, an event, etc, etc) to
indicate that it should stop processing.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Thanks a lot, One more quistion for my general knolage, What
Thread.Abort()
do, I never saw this call cause anything :-( .

Nicholas Paldino said:
Joe,

I wouldn't bother with the AutoSetEvent to determine if your loop
should
still process. Rather, I would just say to have a boolean value which
access is synchronized to, and then check that in your loop.

As far as calling DoEvents in your loop which checks the thread if it
is
alive, why are you doing that? Are you in a UI thread? Also, you really
shouldn't do this, IMO. DoEvents is just poor design.

The ^ideal^ situation, IMO, is to have a delegate that the thread has
access to which is called when the thread is done, and you continue your
work from that delegate. Otherwise, if you have to block another thread
to
wait for your worker thread to finish, I would have some sort of Event
that
the worker thread calls when the loop ends, and then wait on that in the
controlling thread.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
I have 3 questions , remark as Why ??? :

I have 2 threads that run in my application, each thread is a loop with
low
level call :

While(!AutoSetEvent.WaitOne(1,true))
{
DLL_Export_GetData(.)
Do something with this data
}

Know some time I want to Abort those threads , So what I did is to
create
AutoSetEvent , when I want to stop the thread I call the Set event
function.
Then wait in loop until the thread died :
While(thread.IsAlive())
{
Continue;
}
In the last loop I tried to add Application.DoEvent() but this cause my
application to crash. Why ???

The big problem is sometime the thread is go out of the loop but it is
still
Alive Why?????

Do any one know how to do that in deferent and elegant way ????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top