Application dying despite catching exception

S

Shannon Lloyd

Hi,
I've got some code which raises or lowers (ie enables or disables) the
SIP on a PPC in order to allow text to be entered when the focus is in
particular controls (eg text boxes). The current scenario is that the
user checks a checkbox, which raises/enables the SIP and puts the focus
into a particular textbox. The problem occurs when the user *unchecks*
the checkbox - the CheckStateChanged event for this lowers/disables the
SIP, but for some reason an exception is being thrown here. So I
surrounded the call to "inputPanel.Enabled = false;" with a try/catch
clause, which catches the exception and displays a MessageBox for
debugging purposes (so I know that the exception is being caught), but
then the MessageBox immediately vanishes (before any user interaction)
and the entire application exits! Am I missing something here? Isn't the
point of exception handling that it allows the software to *handle* the
exception when it occurs instead of allowing it to ascend up through the
call stack until the runtime terminates the process?
FWIW, the particular block of code looks like this:

try
{
this.inputPanel.Enabled = false;
}
catch (Exception e)
{
MessageBox.Show("Exception disabling SIP: " + e.Message);
}

Oh, and I don't get time to even read the output of e.Message before the
process is killed, so I'm not sure what type of Exception is being
thrown. But that shouldn't matter - "Exception" should catch anything.

Cheers,
Shannon
 
D

Daniel Moth

Post a sample so we can look at it. If the process is on its way out no
messagebox will keep it up.

Cheers
Daniel
 
S

Shannon Lloyd

Daniel said:
Post a sample so we can look at it. If the process is on its way out no
messagebox will keep it up.

Cheers
Daniel

Here is the offending file, as stripped-down as I can make it whilst
still throwing the Exception. I've managed to identify that the SIP
Enabled state change event is throwing an ObjectDisposedException, if
that helps.
The other thing worth mentioning is how this form gets displayed,
because this error is not replicated if I create a standalone
application with just this form in it - in that instance the SIP
functions entirely as expected. My application consists of two paths
with a common starting point. The user follows the first path through a
series of about 4 forms, then closes down each form one at a time to
arrive back at the first form (data gets passed back as each form is
closed via the Form.Closing event). From there, they then take the
second path, which presents them with a single form which summarises
various aspects of information relating to what they entered/selected on
the first path. All of this occurs in a single process, btw. It is this
last form (on the second path) which is causing the problem (and the
fact that the Exception doesn't get thrown if I implement this form as a
standalone app makes me think it has something to do with the
application as a whole). Below is the code for the form:

using Microsoft.WindowsCE.Forms;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Bidding
{
public class LotBidder : Form
{
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.CheckBox checkWon;
private System.Windows.Forms.Label lblPricePaid;
private System.Windows.Forms.Label lblWon;
private Microsoft.WindowsCE.Forms.InputPanel inputPanel;
private System.Windows.Forms.TextBox txtPricePaid;
private System.Windows.Forms.ListView lotListView;

public LotBidder()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.mainMenu = new System.Windows.Forms.MainMenu();
this.inputPanel = new Microsoft.WindowsCE.Forms.InputPanel();
this.lotListView = new System.Windows.Forms.ListView();
this.checkWon = new System.Windows.Forms.CheckBox();
this.lblPricePaid = new System.Windows.Forms.Label();
this.txtPricePaid = new System.Windows.Forms.TextBox();
this.lblWon = new System.Windows.Forms.Label();

this.lotListView.Location = new System.Drawing.Point(4, 18);
this.lotListView.Size = new System.Drawing.Size(232, 220);

this.checkWon.Location = new System.Drawing.Point(70, 245);
this.checkWon.Size = new System.Drawing.Size(20, 20);

this.lblPricePaid.Location = new System.Drawing.Point(98, 247);
this.lblPricePaid.Size = new System.Drawing.Size(80, 20);
this.lblPricePaid.Text = "Price paid $";
this.lblPricePaid.TextAlign = ContentAlignment.TopRight;

this.txtPricePaid.Enabled = false;
this.txtPricePaid.Location = new System.Drawing.Point(180, 244);
this.txtPricePaid.Size = new System.Drawing.Size(56, 20);
this.txtPricePaid.Text = "";

this.lblWon.Location = new System.Drawing.Point(8, 247);
this.lblWon.Size = new System.Drawing.Size(56, 20);
this.lblWon.Text = "Lot won";
this.lblWon.TextAlign = System.Drawing.ContentAlignment.TopRight;

this.Controls.Add(this.lblWon);
this.Controls.Add(this.txtPricePaid);
this.Controls.Add(this.lblPricePaid);
this.Controls.Add(this.checkWon);
this.Controls.Add(this.lotListView);
this.Menu = this.mainMenu;
this.Text = "Lot Bidder";
}
}
}


When I reach this form in the application (after having already
traversed the application's first path), the form loads up and displays
fine, and if I open the SIP, it opens fine. But when I close the SIP
(manually or programatically, it makes no difference) an
ObjectDisposedException gets thrown and the process is killed. The form
gets displayed via a call to new LotBidder() and then calling its Show()
method.

Any ideas? I've already wasted a day and a half on this stupid thing. Argh.

Cheers,
Shannon
 
S

Shannon Lloyd

Shannon said:
Hi,
I've got some code which raises or lowers (ie enables or disables) the
SIP on a PPC in order to allow text to be entered when the focus is in
particular controls (eg text boxes). The current scenario is that the
user checks a checkbox, which raises/enables the SIP and puts the focus
into a particular textbox. The problem occurs when the user *unchecks*
the checkbox - the CheckStateChanged event for this lowers/disables the
SIP, but for some reason an exception is being thrown here. So I
surrounded the call to "inputPanel.Enabled = false;" with a try/catch
clause, which catches the exception and displays a MessageBox for
debugging purposes (so I know that the exception is being caught), but
then the MessageBox immediately vanishes (before any user interaction)
and the entire application exits! Am I missing something here? Isn't the
point of exception handling that it allows the software to *handle* the
exception when it occurs instead of allowing it to ascend up through the
call stack until the runtime terminates the process?
FWIW, the particular block of code looks like this:

try
{
this.inputPanel.Enabled = false;
}
catch (Exception e)
{
MessageBox.Show("Exception disabling SIP: " + e.Message);
}

Oh, and I don't get time to even read the output of e.Message before the
process is killed, so I'm not sure what type of Exception is being
thrown. But that shouldn't matter - "Exception" should catch anything.

Cheers,
Shannon

For the Google archives - I've managed to sort this one out. The issue
seems to be that each form subscribes to the SIP's EnabledChanged event,
but unless you *un*subscribe the form from being notified about this
event (ie in the form's Closed event handler), then the SIP is going to
call the EnabledChanged event handler for that form even after it has
been disposed, because there is only one SIP (not a new one for every
form). For example, if I use the SIP on the main form of my application,
then open a new form and use the SIP on it, then close that second form,
the form gets disposed, but the SIP is still going to send events to its
handle when it is next used on the main form. Putting something along
the following lines into the Closed handler for the form does the trick:

this.inputPanel.EnabledChanged -= new
EventHandler(inputPanel_EnabledChanged);

Cheers,
Shannon
 

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