如何管理包装器 DLL 中的第三方 DLL 事件



我是初学者!我制作了一个第三方 DLL (DLL_B) 的包装器 DLL (DLL_A)。您可以在下面找到一个简单的示例:

Class_B公开的DLL_B类(我只能查看元数据中的签名):

public delegate void eveHandler(bool ret_B);
public class cls_B
{
    public cls_B(string init);
    public event eveHandler eve;
    public void req(eveHandler reqHandler = null);
}

DLL_A内部:

public class cls_A
{     
    private cls_B objClsB;
    private bool continueWorking = true;
    public cls_A()
    {
        objClsB = new cls_B("test");
        objClsB.ev += new eveHandler(this.eveManager);
    }
    public eveManager(bool ret_A)
    {
        continueWorking = false;
    }
    public request()
    {
        objClsB.req();
        int i = 0;
        While (continueWorking && i < 100)
        {
            //Do a lot of stuff...
            i++;
        } 
    }
}

然后在主应用程序中:

cls_A objClsA = new cls_A();
objClsA.request();
MessageBox.Show("Done!", "MyApp");

它可以工作,但似乎只有在执行MessageBox.Show("Done!", "MyApp");之前,才在退出objClsA.request();时调用eveManager()。事实上,如果我删除&& i < 100部分,它将卡在 while 循环中,但我需要事件停止循环。

我错在哪里?

提前感谢!

解决方案成立!如果我以这种方式在另一个线程上启动objClsB.req(),它会起作用:

public request()
{
    Thread thr = new Thread(delegate() { objClsB.req(); });
    thr.Start();        
    int i = 0;
    While (continueWorking)
    {
        //Do a lot of stuff...
    } 
}

最新更新