mfc >> Preventing application exit

by Al » Wed, 03 Dec 2003 19:45:58 GMT

I want to prevent the user from existing my application
if it has a long-running thread in operation. How would I
do this?

I assume the thread needs to send a message somewhere (to
the main frame window?) when it starts, to tell the
application to resist closing. The main frame's OnClose()
handler would then check a variable somewhere to decide
whether to close or not.

But where do I send the message and how do I catch it? I
don't think I understand the message system very well,
which is a bit of a problem when programming Windows!

--- Al.


mfc >> Preventing application exit

by Al » Wed, 03 Dec 2003 20:16:28 GMT



Think I've found it. A quick look at PostThreadMessage
and ON_THREAD_MESSAGE in MSDN revealed that my message
handler was in the wrong message map macro. It now works.

--- Al.

I
(to
()



mfc >> Preventing application exit

by Ali R. » Thu, 04 Dec 2003 00:21:09 GMT

Or your close can simply ask the thread if it's busy or not.

Ali R.







Preventing application exit

by Scott McPhillips [MVP] » Thu, 04 Dec 2003 09:31:12 GMT





You should not use PostThreadMessage to a GUI thread - the message can
be lost (a documented limitation). Use PostMessage. There is a step by
step example of posting to the GUI thread in:

http://www.mvps.org/vcfaq/

--
Scott McPhillips [VC++ MVP]



Similar Threads

1. Preventing an application exit from the task bar's right click Close menu

2. Application.exit() vs Environment.exit(-1) vs Application.exitthread()

I am trying to close my application using Application.exit() in the
frmMain_Closing event. When the form closes the process does not.  My
application only has one form (no other classes either).  The form has
a really long loop which generates approx. 700 .csv files.  I do not create
any threads myself (Application.exitthread() doesn't work either).  
To counteract this I have decided to use the Environment.exit(-1)
method instead. 

What is the difference between the three methods?

Using environment.exit() seems a strange way to do things considering there
is a Application.exit method which claims to do what the Environment.exit()
method does.

3. Application.Exit() not exiting - CSharp/C#

4. Application.Exit() vs Environmrnt.Exit(0)

Hi All,
When I use applcation.exit() in winForm application, the form closed, but 
the process is still going!! ( The debug process is still running if debug in 
VS IDE). Environment.Exit(0) works fine. But how to do in such following 
scenario: if I need to give 2 option for user,1. Quit 2. Restart.  In Quit 
option, I use Environment.Exit(0) to confirm the process will be stopped. In 
Restart option, I can't use Environment.Exit(0) , because following 
application.restart() can't execute never. but if i use Application.Exit(), 
the application can restart, but if the user choose quit next time. alas! the 
process is going!  it won't stop this time.
Hope my desception is clear :)
Pls Help!!!

5. Application::Exit() vs Environment::Exit() in .NET multithreading

6. Preventing an exit

I've created some code (without using the form builder) that is extremely
simple. All it does is, in it's constructor, create a NotifyIcon and make it
visible. It also has a ContextMenu that quits the application.

The problem is, while the icon does display in the system tray when I run
the application, it disappears as soon as I put the mouse over it. On closer
inspection, the program just opens then closes immediately - the icon only
disappears when I make Windows update the tray by moving my mouse onto the
taskbar!

I want my app to stay open until I tell it to close.

I was advised to add a Thread that just yields in its loop, to block the
constructor from exiting, but isn't that a workaround? I want my app to lay
dormant until an event occurs (like the user hitting 'Quit' form the
contextmenu). Surely the framework has some method of achieving that?

Any advice hugely appreciated.

- Dave.


7. Preventing immediate exit of MFC app

8. Application.Run() Application.Exit and the 'Message Pump'

Hi Dave

Thanks for your response - apologies for taking so long to reply, I
decided to take a detour to Threading, as I thought that might help me
with this topic.

With your response and my detour to Threading, I'm more comfortable,
I just have 3 more 'itty bitty' questions that I hope will clear
the confusion I have over Application.Exit() ....

As before, set up a project with one form, and that form just has a
close button.  In the close button's 'on click' event handler,
just type:
  this.close();

Add a class to the project and put in the start routine:

  public class StartUp
  {
    static void Main()
    {
      //This should 'block' until the user clicks the close button
in the form
      Application.Run(new ExForm());
      //Should this second call to Application.Run work ?  It does!
      Application.Run(new ExForm());
      Debug.WriteLine("Finished Main");
    }
  }

At this point the application should consist of a form - ExForm, and
a StartUp class.

(1)

In the above example, you should find that a form shows, and code
execution is "blocked" until you click close.  After clicking
close, a second form shows, and code execution 'blocks' until you
click close.  Then finally, the application terminates.  Confirmation
Question - The closing of the first form does not 'mortally
wound' the message pump and it can get started again for the second
call to Application.Run?

(2)

Keep everything the same, but in the form, change the close button's
'on click' event handler to:
  Application.Exit();

In this example, you should find that the first form shows, and code
execution is "blocked" until you click close.  After clicking
close, a second form "flashes" on and off the screen.  Confirmation
Question - The closing of the first form causes
'Application.Exit()' which seems to 'mortally wound' the
message pump.  When the second call to Application.Run occurs, the
message pump cannot be revived, and therefore code execution cannot be
blocked.  Execution falls through to the debug.WriteLine and then the
end of static void Main is reached, causing the application to
terminate.

(3)

With the close button's 'on click' event handler still calling
'Application.Exit', put this code into the StartUp class:

  public class StartUp
  {
    static void Main()
    {
      ExForm myExForm = new ExForm();
      myExForm.Show();
      Application.Run(); //This call to Application.Run should block
      //Should this second call to Application.Run work ?  It does!
      Application.Run(new ExForm());
      Debug.WriteLine("Finished Main");
    }
  }

In this above example, you should find that a form shows, and code
execution is "blocked" until you click close.  After clicking
close, a second form shows, and code execution 'blocks' until you
click close.  Then finally, the application terminates.

Unlike scenario (2), in this scenario the message pump is not
'mortally wounded ????  Is this something to do with the fact that we
show the form then call Application.Run() with no parameters ????  Que
!?!

Cheers
Bill