D
david.dearing
I have a Form that consists of multiple views with corresponding
controllers (MVC). The presenters are listening to events that may
come from non-UI threads. I am currently using InvokeRequired and
BeginInvoke to handle the events appropriately.
generic code from controller.cs:
public void OnChange(EventArgs args)
{
if (_view.InvokeRequired)
{
_view.BeginInvoke(new OnChange(OnChange), new object []
{args});
}
else
{
// ... handle event args
}
}
When I close the main Form (via the "x" in the corner), the controller
is still receiving events (from a Service that doesn't close). I have
found that once the Form begins to close, InvokeRequried doesn't return
the correct result and the application blocks. (More info here:
http://pdxjjb.blogspot.com/2003_08_01_pdxjjb_archive.html#106047473215550135).
To remedy this, I have tried adding logic to determine if the view or
any of its Parent controls (the view is nested within the Form) are
IsDispose-ing or Dispose-d. This works for several calls (which I've
seen via logging) but will still block in some cases. The code I added
is:
private bool CheckViewDisposal(Control c)
{
if (c == null) return false;
return c.IsDisposed || c.Disposing || Disposing(c.Parent);
}
public void OnChange(EventArgs args)
{
if (CheckViewDisposal(_view)) return;
if (_view.InvokeRequired)
{
...
What am I missing? Why does the view return false for InvokeRequired
as well as its Disposal state when the handler is called from a non-UI
thread?
Any thoughts? Thanks!
controllers (MVC). The presenters are listening to events that may
come from non-UI threads. I am currently using InvokeRequired and
BeginInvoke to handle the events appropriately.
generic code from controller.cs:
public void OnChange(EventArgs args)
{
if (_view.InvokeRequired)
{
_view.BeginInvoke(new OnChange(OnChange), new object []
{args});
}
else
{
// ... handle event args
}
}
When I close the main Form (via the "x" in the corner), the controller
is still receiving events (from a Service that doesn't close). I have
found that once the Form begins to close, InvokeRequried doesn't return
the correct result and the application blocks. (More info here:
http://pdxjjb.blogspot.com/2003_08_01_pdxjjb_archive.html#106047473215550135).
To remedy this, I have tried adding logic to determine if the view or
any of its Parent controls (the view is nested within the Form) are
IsDispose-ing or Dispose-d. This works for several calls (which I've
seen via logging) but will still block in some cases. The code I added
is:
private bool CheckViewDisposal(Control c)
{
if (c == null) return false;
return c.IsDisposed || c.Disposing || Disposing(c.Parent);
}
public void OnChange(EventArgs args)
{
if (CheckViewDisposal(_view)) return;
if (_view.InvokeRequired)
{
...
What am I missing? Why does the view return false for InvokeRequired
as well as its Disposal state when the handler is called from a non-UI
thread?
Any thoughts? Thanks!