The application I'm working on stores all information in-memory due to
security restrictions. One of the problems we are having is other
programs calling SHCloseApps() to free up memory. This will in extreme
cases shut down our application, which effectively means that all
stored information is lost.

To prevent this from happening we have tried to catch the WM_CLOSE
message coming from SHCloseApps. Catching the message works perfectly
through the Application2 class in OpenNETCF. What doesn't work as well
is cancelling the event, as the application exits anyway.

Can anyone tell me if this happens because of the way SHCloseApps
works? Is for instance TerminateProcess called when the application
doesn't close?

The test program is as follows:

----- Program catching WM_CLOSE -----

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
FilterCloseMsg filterCloseMsg = new FilterCloseMsg();
Application2.AddMessageFilter(filterCloseMsg);
Application2.Run(new Form1());
}
}

public class FilterCloseMsg : IMessageFilter
{
private const int WM_CLOSE = 0x0010;

public bool PreFilterMessage(ref Message m)
{
switch (m.Msg)
{
case WM_CLOSE:
{
return true;
}
}

return false;
}
}

------ Method called from another application to free memory -----

[DllImport("aygshell.dll")]
public static extern int SHCloseApps(int dwMemSought);

private void buttonCloseApps_Click(object sender, EventArgs e)
{
SHCloseApps(20 * 1048576); // Test: Try to acquire 20 MB of
memory
}



Hope you can help me.

Best regards,
Tommy Wendelborg

Re: Cancelling a WM_CLOSE using Application2 of the Smart Device Framework by Peter

Peter
Sun Oct 01 03:51:04 CDT 2006

Handle the Closing event in your main form and set the Cancel property of
the passed CancelEvemtArgs to true. See this example:-
http://www.peterfoot.net/OpenAllHours.aspx

Peter

--
Peter Foot
Device Application Development MVP
www.peterfoot.net | www.inthehand.com

<wendelborg@gmail.com> wrote in message
news:1159538471.945170.195280@e3g2000cwe.googlegroups.com...
> The application I'm working on stores all information in-memory due to
> security restrictions. One of the problems we are having is other
> programs calling SHCloseApps() to free up memory. This will in extreme
> cases shut down our application, which effectively means that all
> stored information is lost.
>
> To prevent this from happening we have tried to catch the WM_CLOSE
> message coming from SHCloseApps. Catching the message works perfectly
> through the Application2 class in OpenNETCF. What doesn't work as well
> is cancelling the event, as the application exits anyway.
>
> Can anyone tell me if this happens because of the way SHCloseApps
> works? Is for instance TerminateProcess called when the application
> doesn't close?
>
> The test program is as follows:
>
> ----- Program catching WM_CLOSE -----
>
> static class Program
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [MTAThread]
> static void Main()
> {
> FilterCloseMsg filterCloseMsg = new FilterCloseMsg();
> Application2.AddMessageFilter(filterCloseMsg);
> Application2.Run(new Form1());
> }
> }
>
> public class FilterCloseMsg : IMessageFilter
> {
> private const int WM_CLOSE = 0x0010;
>
> public bool PreFilterMessage(ref Message m)
> {
> switch (m.Msg)
> {
> case WM_CLOSE:
> {
> return true;
> }
> }
>
> return false;
> }
> }
>
> ------ Method called from another application to free memory -----
>
> [DllImport("aygshell.dll")]
> public static extern int SHCloseApps(int dwMemSought);
>
> private void buttonCloseApps_Click(object sender, EventArgs e)
> {
> SHCloseApps(20 * 1048576); // Test: Try to acquire 20 MB of
> memory
> }
>
>
>
> Hope you can help me.
>
> Best regards,
> Tommy Wendelborg
>



Re: Cancelling a WM_CLOSE using Application2 of the Smart Device Framework by wendelborg

wendelborg
Mon Oct 02 05:21:24 CDT 2006

Thanks for the quick response.

Unfortunately I'm still having trouble. I have tested a bit without the
message filter, but my test program still exits using
cancelEventArgs.Cancel set to true.

The simple test solutions can be found here:

http://www.lobotommy.com/WM_CLOSE/Application2Test.zip
http://www.lobotommy.com/WM_CLOSE/Killer.zip

Application2Test is set up to catch both WM_CLOSE and Form.Close(). A
note to the Killer application, my device normally has less than 20 MB
of program memory available. Killer is set up to ask for 20 MB of free
memory through SHCloseApps().

During debug both the message filter and close event approaches fire,
but the application closes down regardless.

Any help will be of great value to us. It seems like other people get
it to work, so it would be very kind if somebody could spot our
mistakes.


Best Regards,
Tommy Wendelborg