473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Exiting a Thread

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;
}
}
Nov 15 '05 #1
8 9564
Does Join not work for your needs?

--
William Stacey, MVP

"Maloney" <sa*******@softhome.net> wrote in message
news:#x**************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 15 '05 #2
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]
- mv*@spam.guard.caspershouse.com

"Maloney" <sa*******@softhome.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 15 '05 #3
Join is not supported by the CompactFramework. Thanks though

"William Stacey" <st***********@mvps.org> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
Does Join not work for your needs?

--
William Stacey, MVP

"Maloney" <sa*******@softhome.net> wrote in message
news:#x**************@tk2msftngp13.phx.gbl...
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;
}
}


Nov 15 '05 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Maloney" <sa*******@softhome.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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;
}
}


Nov 15 '05 #5
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]
- mv*@spam.guard.caspershouse.com

"Maloney" <sa*******@softhome.net> wrote in message
news:u7**************@TK2MSFTNGP12.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Maloney" <sa*******@softhome.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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;
}
}



Nov 15 '05 #6
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

"Maloney" <sa*******@softhome.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 15 '05 #7
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/downlo...ampleSetup.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/p...Q/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.
"Maloney" <sa*******@softhome.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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;
}
}

Nov 15 '05 #8
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.

--
William Stacey, MVP

"Maloney" <sa*******@softhome.net> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
Join is not supported by the CompactFramework. Thanks though

"William Stacey" <st***********@mvps.org> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
Does Join not work for your needs?

--
William Stacey, MVP

"Maloney" <sa*******@softhome.net> wrote in message
news:#x**************@tk2msftngp13.phx.gbl...
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;
}
}



Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
I have a class that is sometimes called from the command line and sometimes from another class. When the user clicks "Quit" on the panel it opens, if it is running from the command line, it...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
3
by: Bonj | last post by:
When trying to debug an ASP.NET project by placing a breakpoint in one of the functions in the codebehind, VS correctly stops at the breakpoint, and I can step through. But soon after, the process...
5
by: Tim Werth | last post by:
I have a .NET console application that is kicked off by a .NET Windows service. They communicate via .NET Remoting, although there isn't much communication between the two while the console app is...
6
by: Ant | last post by:
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the...
2
by: jrpfinch | last post by:
Is the following the most elegant way to exit a multi-threaded application on a Ctrl-C? I am a complete beginner and would have thought there was some way of doing it without having to use while...
5
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has...
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.