"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
news:#1*************@TK2MSFTNGP12.phx.gbl:
You might be able to get around it by using a bit of code in the
appdomain that can catch and gracefully handle exceptions, just place
a little code between the plugin client and the client itself that
handles that.
Hi Daniel,
I figured someone might answer with this, but I was hoping that there
would be a more straightforward answer...
The problem is that I (the hosting application) never call the plugin
code. I pass it a pointer to services that the hosting application
provides, like this:
interface IPluginHost
{
void SetHostTitle(string text);
}
interface IPlugin
{
Initialize(IPluginHost host);
}
public class MyPlugin : IPlugin
{
public void Initialize(IPluginHost host)
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(this.DoPluginWork)
);
}
public void DoPluginWork(object state)
{
Thread.Sleep(10000);
host.SetTitle("blah");
// This kills the entire application
throw new Exception();
}
}
public class Form1 : Form, IPluginHost
{
// normal windows form stuff
public void SetHostTitle(string text)
{
// In my host app, this is Invoked to the UI thread
this.Text = text;
}
public Form1()
{
InitializeComponent();
IPlugin myPlugin = new MyPlugin(this);
}
}
Obviously things are more complicated than I show here, but you get the
idea.... Now if I was calling IPlugin functions from my host, then I
could wrap them in a try/catch, but as you can see, that's not the way I
want to do it. I want the plugins to be in charge of their own destiny,
and not have to wait on a 'polling' mechanism from the host to affect
changes in the host.
As you can see, I have no way of putting a try/catch around the work
that the client does from the code in Form1. Of course I could put it
around the code INSIDE DoPluginWork, but that's something I can't
control if other programmers are writing an IPlugin for my application.
Thus I'm looking for some way to prevent THEIR bad programming from
destroying my entire application. I thought that AppDomains would do
this, but that's not what my experience is so far - an exception thrown
in another AppDomain still kills the entire application.
-mdb