c# - 如何监控插件工作

标签 c# multithreading events plugins delegates

我的团队正在开发一个插件项目,其中主机应用程序协调插件工作(每个插件都有特定的功能,将在单独的线程上执行)。我正在编写主机应用程序并定义 IPlugin 接口(interface);其他人将开发插件。

问题是:如何知道插件何时完成或抛出异常。我有一个解决方案是使用事件和委托(delegate)让插件回调主机应用程序,但我认为这种方法并不是很好。因为如果开发插件的人实现了我的 IPlugin 接口(interface)但忘记编写引发事件的代码。

在这种情况下,插件可以插入我的主机应用程序,但我的主机应用程序无法知道此插件何时完成或抛出异常或其他通信,这非常糟糕。

我的代码是这样的:

IPlugin.cs:

public delegate void OnFinishDelegate(IPlugin p, AMessageEventArgs e);
public delegate void OnExceptionDelegate(IPlugin p, AMessageEventArgs e);
public interface IPlugin
{
    event OnFinishDelegate OnFinish;
    event OnExceptionDelegate OnException;
    void DoWork();
}

事件参数:

public class AMessageEventArgs:EventArgs
{
    private string message;
    public string Message
    {
        get { return message; }
        set { message = value; }
    }
}

主机应用:

static void Main(string[] args)
        {
            // ...ignore load plugin code
            //
            IPlugin p = new Plugin();            

            // register event with IPlugin
            p.OnFinish += new OnFinishDelegate(OnFinishHandler);
            p.OnException += new OnExceptionDelegate(OnExceptionHandler);

            // let plugin do its work on a subthread
            Thread t = new Thread(p.DoWork);
            t.Start();

            // and then main continue do other work...
        }

        // if plugin throw exception, in the host application will 
        // handle this exception...
        private static void OnExceptionHandler(IPlugin p, AMessageEventArgs e)
        {
            // in here will process exception of plugin...
        }

        // if plugin completed its work, it will use OnFinish event to 
        // notify to host application
        private static void OnFinishHandler(IPlugin p,AMessageEventArgs e)
        {
            // in here will process completed work event
        }

我希望插件代码如下所示:

public class Plugin:IPlugin
    {
        // use event to callback host application
        public event OnFinishDelegate OnFinish;
        public event OnExceptionDelegate OnException;

        // Create an EventArgs
        AMessageEventArgs e = new AMessageEventArgs();
        public void DoWork()
        {
            try
            {
                // execute update data
                UpdateData();
            }
            catch (Exception ex)
            {
                e.Message = ex.Message;

                // if have any exception, Plugin will raise an event in host application
                // but if developer forget write below three lines of code, my host application will
                // out of control.
                if (OnException!=null)
                {
                    OnException(this,e);
                }
            }

            // if have no exception, Plugin will use OnFinish event to talk to host application
            // to know that this plugin completed its work
            // but if developer forget write below three lines of code, my host application will
            // out of control.
            if (OnFinish!=null)
            {
                OnFinish(this,e);
            }
        }

如何解决这个问题?

附加问题:我的 IPlugin 接口(interface)定义好了吗?如果不好,你能建议我改进这个界面吗?

谢谢!

最佳答案

我认为插件不需要知道它们在单独的线程中运行。我只想:

interface IPlugin { 
    void DoWork(); 
}

然后宿主程序将处理捕获异常和所有线程,比如:

Thread t = new Thread(() => {
    try {
        plugin.DoWork();
    }
    catch (Exception ex) {
        // handle exception or save it to handle in the main thread
    }
});
t.Start();

关于c# - 如何监控插件工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29640431/

相关文章:

c# - 如何在使用 Bouncy CaSTLe 加密之前填充数据

c# - 为每个项目中的字段赋值的扩展方法?

c# - WPF Datagrid 鼠标双击编辑单元格

java - id = 1 - id 是原子的吗?

javascript - 在新插入的元素上应用 javascript 库

c# - 变更通知的最佳模式(事件或委托(delegate))

c# - 定义和调用函数

c++ - 如何让 boost::thread_group 执行固定数量的并行线程

javascript - 像 Node.js 一样并行请求或排队

Javascript - 触发操作或事件?