Exiting a Thread

M

Maloney

Exit thread in C#CF. I'm createing a thread and setting a class object for
when to exit. Before creating the thread i check if the thread is running,
if so set the flag to exit the thread.

What is the proper way of waiting for the thread to exit. In C++ i would
set an event and WaitForMultipleObject, peek and pump messages until the
handle was set.

Class test
{
private bool ExitThread = false
private bool ThreadRunning = false
private Thread myThread

private StartThread()
{
if(ThreadRunning)
{
ExitThread = true;
}
myThread = new Thread(new ThreadStart(FuncThread));
}

private FuncThread()
{
ThreadRunning = true;
while(more files)
{
if(ExitThread)
break;

read files in directory
Invoke back to main thread and fill item to list box
}
ThreadRunning = false;
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Maloney,

Unfortunately, there isn't any mechanism in .NET to be notified when a
thread is complete. In order to do this, you will have to have the code in
the thread send some sort of notification when it is complete. One
recommendation would be to use a try/catch/finally block to call Set on a
ManualResetEvent which would be waited on by another thread.

Also, it should be noted from the code that you provided that you aren't
providing the proper synchronization to the ExitThread field on your class.
You should be wrapping access to this in a lock statement.

Hope this helps.
 
M

Maloney

Thanks for the info Nicholas. When you say lock are you talking about
Monitor.Enter, Monitor.Exit or just lock. What am i locking, is this
correct.

lock(myThread)
{
ExitThread = true;
}

lock(myThread)

{

if(ExitThread)
break;

}


Nicholas Paldino said:
Maloney,

Unfortunately, there isn't any mechanism in .NET to be notified when a
thread is complete. In order to do this, you will have to have the code in
the thread send some sort of notification when it is complete. One
recommendation would be to use a try/catch/finally block to call Set on a
ManualResetEvent which would be waited on by another thread.

Also, it should be noted from the code that you provided that you aren't
providing the proper synchronization to the ExitThread field on your class.
You should be wrapping access to this in a lock statement.

Hope this helps.


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

Maloney said:
Exit thread in C#CF. I'm createing a thread and setting a class object for
when to exit. Before creating the thread i check if the thread is running,
if so set the flag to exit the thread.

What is the proper way of waiting for the thread to exit. In C++ i would
set an event and WaitForMultipleObject, peek and pump messages until the
handle was set.

Class test
{
private bool ExitThread = false
private bool ThreadRunning = false
private Thread myThread

private StartThread()
{
if(ThreadRunning)
{
ExitThread = true;
}
myThread = new Thread(new ThreadStart(FuncThread));
}

private FuncThread()
{
ThreadRunning = true;
while(more files)
{
if(ExitThread)
break;

read files in directory
Invoke back to main thread and fill item to list box
}
ThreadRunning = false;
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Maloney,

In C#, when you use the lock statement like this:
lock (myThread)
{
// Code statements here.
}

It actually expands to this:

Monitor.Enter(myThread)

try
{

}
finally
{
Monitor.Exit(myThread);
}

So in essence, the lock statement handles the calls to the static Enter
and Exit methods on the Monitor class.


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

Maloney said:
Thanks for the info Nicholas. When you say lock are you talking about
Monitor.Enter, Monitor.Exit or just lock. What am i locking, is this
correct.

lock(myThread)
{
ExitThread = true;
}

lock(myThread)

{

if(ExitThread)
break;

}


message news:%[email protected]...
Maloney,

Unfortunately, there isn't any mechanism in .NET to be notified when a
thread is complete. In order to do this, you will have to have the code in
the thread send some sort of notification when it is complete. One
recommendation would be to use a try/catch/finally block to call Set on a
ManualResetEvent which would be waited on by another thread.

Also, it should be noted from the code that you provided that you aren't
providing the proper synchronization to the ExitThread field on your class.
You should be wrapping access to this in a lock statement.

Hope this helps.


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

Maloney said:
Exit thread in C#CF. I'm createing a thread and setting a class
object
for
when to exit. Before creating the thread i check if the thread is running,
if so set the flag to exit the thread.

What is the proper way of waiting for the thread to exit. In C++ i would
set an event and WaitForMultipleObject, peek and pump messages until the
handle was set.

Class test
{
private bool ExitThread = false
private bool ThreadRunning = false
private Thread myThread

private StartThread()
{
if(ThreadRunning)
{
ExitThread = true;
}
myThread = new Thread(new ThreadStart(FuncThread));
}

private FuncThread()
{
ThreadRunning = true;
while(more files)
{
if(ExitThread)
break;

read files in directory
Invoke back to main thread and fill item to list box
}
ThreadRunning = false;
}
}
 
1

100

Hi Maloney,
If you want to pump events you can do it with Application.DoEvents.
If you want to be notified when the thread exites you can use
Delegate.BeginInvoke and pass AsyncCallback (It should be supported by CF).

However the example you provied has a big flaw.
You don't guard ExitThread and ThreadRunning flags.
So, what happens here is:
If a thread is already running you set ExitThread to true and start a new
thread. Imagine that the new thread starts before the old one exits. The new
one sets ThreadRunning to true then the old one exits and set it to false.
Now you have one running thread and you don't know about it.

More things can happen if you use flags and you don't synchronize the access
to them.

B\rgds
100
 
G

Geoff Schwab [MSFT]

Hi Maloney,

I have a sample at the following location. In this sample, a form starts
several threads and then waits for them to close before exiting. The
whitepaper is being prepped for publication so only the code is available
but it is really simple:

http://download.microsoft.com/downl...c-b6bc-0c1dd962f7b7/StopThreadSampleSetup.exe

It essentially waits on a variable and then overrides OnClosing to cancel
the exit until all threads are closed.

--
Geoff Schwab
Program Manager
Excell Data Corporation
http://msdn.com/mobility
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

William Stacey

You could also probably use a lock to wait. This lock will be seperate from
the sync lock above. When enter thread, lock a ref var that can be accessed
from manager (i.e. public property.) The manager can wait on the lock.
When the thread is done, it exits the lock block and manager will get the
lock and do what ever.
 

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