CF 2.0 : How to exit the application from a separate thread ?

S

Steve B.

Hi,

I have an application with some forms.
One of this forms use a background process to perform some operations. The
process is in a second thread, in order to display the user a progress bar.
From this process in a second thread, I sometimes require closing the
application. How can I close the app ?
FYI, the process is a quite complex process (a list of jobs, each job are in
a separate class library and a job manager handle it) so it's quit difficult
to "return" a boolean to tell the app to close.

I've tried an application.exit, but the application continues to run...
I could use Process.GetCurrentProcess.Kill(), but I'm afraid there's side
effect.

I'd appreciate any advise to reach my goal.

Thanks,
Steve
 
E

Ebbe Kristensen

Steve said:
One of this forms use a background process to perform some
operations. The process is in a second thread, in order to display
the user a progress bar.

Okay, so you have some kind of communication between the main thread (where
the forms are) and the processing thread. How do you do that?
From this process in a second thread, I
sometimes require closing the application. How can I close the app ?

AFAIK you can't. Only the main thread can do that. So you need to make the
processing thread tell the main thread to close down. You should probably
have the main thread shut down
I could use Process.GetCurrentProcess.Kill(), but I'm afraid there's
side effect.

There is indeed: the process gets killed, no questions asked. If it has
files opened, unsaved data etc. ect., bad luck.

Ebbe
 
G

Guest

Set an event or call a method that closes the main form passed into
Application.Run, then exit the thread.
 
S

Steve B.

I've build a small component that acts like the backgroundworker component
in desktop applications.
This component runs the process in a separate thread and subscribe to the
process events.
Each events of the processes are captured and the component raise its own
events from the host form using myform.invoke.

Do you advise me to add a new event in my process like 'RequestExit', and
make the form aware of this event to do an Application.Exit ?
Do I have to also close the thread or does application.exit is enough ?

Thanks,
Steve
 
S

Steve B.

Thanks, that answer my new question...


Set an event or call a method that closes the main form passed into
Application.Run, then exit the thread.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
 
E

Ebbe Kristensen

Steve said:
Do you advise me to add a new event in my process like 'RequestExit',
and make the form aware of this event to do an Application.Exit ?

Yup, something like that.
Do I have to also close the thread or does application.exit is enough?

I'd close the thread just to be on the safe side.

Ebbe
 

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