ApplicationEx.ShowDialog will throw ObjectDisposedException

  • Thread starter =?ISO-8859-1?Q?Linus_R=F6rstad?=
  • Start date
?

=?ISO-8859-1?Q?Linus_R=F6rstad?=

Hi!

I'm have a bit of a problem with the
OpenNETCF.Windows.Forms.ApplicationEx.ShowDialog() function when using
it with the IMessageFilter class. I have created a class which inherites
from the IMessageFilter class to get WM messages for all the forms in
my application. This works great to get the messages but I have
discovered some problems when I want to use it on all the forms.

I start my application by using ApplicationEx.Run to display my main
form. I call ApplicationEx.AddMessageFilter to add my form and register
a function to recieve the messages for this form. When I push a button
on the main form it will display a second form by using
ApplicationEx.ShowDialog and I will also register this form to recieve
messages. So far everything is great. I will recive the messages in the
second form too. But when I close the second form the application will
throw two ObjectDisposedExceptions.

I have tried to solve this by setting the disposeForm parameter to false
when calling ShowDialog and this works but the form is then not closed,
only hidden behind the main form. And when I close the second form from
the main form I still get the ObjectDisposedExceptions.

I have also tried to remove the messagefilter in the main form before
calling ShowDialog without success. I have DialogResult set before
closing the second form.

Any solution to this?

Sincerely
Linus Rörstad
 
G

Guest

Alex said:
Could you show your code?

Here is the most important parts of the code:

public static void Main()
{
try
{
ApplicationEx.Run(new FormMain());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

public class FormMain : System.Windows.Forms.Form
{
private WinProcFilter winproc;
public FormMain()
{
InitializeComponent();
winproc = new WinProcFilter(this);
OpenNETCF.Windows.Forms.ApplicationEx.AddMessageFilter(winproc);
winproc.WndProc += new WinProc(FormMain_WinProc);

}

private void FormMain_WinProc(Keys pushed)
{
if(pushed == Keys.Esc)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}

private void button1_Click(object sender, System.EventArgs e)
{
FormReport formreport = new FormReport();
OpenNETCF.Windows.Forms.ApplicationEx.ShowDialog(formreport);

}
}

public class FormReport : System.Windows.Forms.Form
{
private WinProcFilter winproc;
public FormReport()
{
InitializeComponent();
winproc = new WinProcFilter(this);
OpenNETCF.Windows.Forms.ApplicationEx.AddMessageFilter(winproc);
winproc.WndProc += new WinProc(FormReport_WinProc);
}

private void FormReport_WinProc(Keys pushed)
{
if(pushed == Keys.Esc)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}

public class WinProcFilter: IMessageFilter
{
public WinProc WndProc;
private IntPtr handle;

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

public const int WM_CHAR = 0x0102;
public const int WM_KEYUP = 0x0101;

public WinProcFilter(Control ctrl)
{
ctrl.Capture = true;
this.handle = GetCapture();
ctrl.Capture = false;
}

public bool PreFilterMessage(ref Message m)
{
bool handled = false;
if(WndProc != null)
{
if(m.HWnd == handle)
{
if(m.Msg == WM_KEYUP)
{
WndProc((Keys)m.WParam.ToInt32());
}
}
}
return handled;
}

}

Sincerely
Linus Rörstad
 
G

Guest

I've not run it, but looking at it I see a potential problem. You catch trhe
escape key in the filter, then close your form based on that. However that
escap[e key is still going to get posted and dispatched (because you didn't
set Handled = true). So your parent will probably receive the Esc after the
child closes, which may in turn cause it to close, or worse the system may
try to dispatch it to the child, which no loger exists (which may give the
error you're seeing).


-Chris



Linus Rörstad said:
Alex said:
Could you show your code?

Here is the most important parts of the code:

public static void Main()
{
try
{
ApplicationEx.Run(new FormMain());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

public class FormMain : System.Windows.Forms.Form
{
private WinProcFilter winproc;
public FormMain()
{
InitializeComponent();
winproc = new WinProcFilter(this);
OpenNETCF.Windows.Forms.ApplicationEx.AddMessageFilter(winproc);
winproc.WndProc += new WinProc(FormMain_WinProc);

}

private void FormMain_WinProc(Keys pushed)
{
if(pushed == Keys.Esc)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}

private void button1_Click(object sender, System.EventArgs e)
{
FormReport formreport = new FormReport();
OpenNETCF.Windows.Forms.ApplicationEx.ShowDialog(formreport);

}
}

public class FormReport : System.Windows.Forms.Form
{
private WinProcFilter winproc;
public FormReport()
{
InitializeComponent();
winproc = new WinProcFilter(this);
OpenNETCF.Windows.Forms.ApplicationEx.AddMessageFilter(winproc);
winproc.WndProc += new WinProc(FormReport_WinProc);
}

private void FormReport_WinProc(Keys pushed)
{
if(pushed == Keys.Esc)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}

public class WinProcFilter: IMessageFilter
{
public WinProc WndProc;
private IntPtr handle;

[DllImport("coredll.dll")]
private static extern IntPtr GetCapture();

public const int WM_CHAR = 0x0102;
public const int WM_KEYUP = 0x0101;

public WinProcFilter(Control ctrl)
{
ctrl.Capture = true;
this.handle = GetCapture();
ctrl.Capture = false;
}

public bool PreFilterMessage(ref Message m)
{
bool handled = false;
if(WndProc != null)
{
if(m.HWnd == handle)
{
if(m.Msg == WM_KEYUP)
{
WndProc((Keys)m.WParam.ToInt32());
}
}
}
return handled;
}

}

Sincerely
Linus Rörstad
 

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