消息泵和AppDomain [英] Message Pumps and AppDomains

查看:88
本文介绍了消息泵和AppDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#(FFx 3.5)应用程序,可将DLL加载为插件.这些插件被加载到单独的AppDomain中(出于很多充分的理由,并且此体系结构无法更改).一切都很好.

I have a a C# (FFx 3.5) application that loads DLLs as plug-ins. These plug-ins are loaded in separate AppDomains (for lots of good reasons, and this architecture cannot change). This is all well and good.

我现在需要显示其中一个插件的对话框.请记住,我不能将对话框的Form返回到主应用程序并在其中显示它(当前基础结构不支持它).

I now have a requirement to show a Dialog from one of those plug-ins. Bear in mind that I cannot return the dialog Form to the main application and have it displayed there (the current infrastructure doesn't support it).

失败1

在我的DLL中,我创建了一个表单并称为Show.对话框轮廓出现了,但是没有画出来,也没有响应鼠标事件.我以为这是因为DLL位于单独的AppDomain中,并且该应用程序的消息泵无法以某种方式将消息分发到新的Form.

In my DLL I created a Form and called Show. The dialog outline showed up but did not paint and it doesn't respond to mouse events. I assumed that this is becasue the DLL is in a separate AppDomain and the message pump for the app is somehow unable to dispatch messages to the new Form.

失败2

在我的DLL中,我创建了一个名为ShowDialog的窗体,按所有权利,该对话框应该为该对话框创建一个内部消息泵.显示该对话框并响应单击(万岁),但是主应用程序似乎不再正在处理或调度Windows消息,因为它退出绘画并且不再响应鼠标事件.由于某种原因,现在看来主应用程序的消息泵未在分派.

In my DLL I created a Form and called ShowDialog, which by all rights should create an internal message pump for the dialog.. The dialog is displayed and responded to clicks (hooray), but it appears that the primary app no longer is processing or dispatching windows messages because it quits painting and no longer responds to mouse events. For some reason now it seems that the main app's message pump is not dispatching.

失败3

在我的DLL中,我创建了一个窗体,名为Application.Run.这肯定会创建一个完整的第二个消息泵.我得到与失败2相同的行为-对话框起作用,但调用应用程序却不起作用.

In my DLL I created a Form and called Application.Run. This will certainly create a complete second message pump. I get the same behavior as Failure 2 - the Dialog behaves, but the calling app does not.

对于这里到底发生了什么以及如何显示来自另一个AppDomain的DLL的对话框,让调用者和被调用者仍然能够正确响应和绘制的内容,有任何想法吗?

Any thoughts on what exactly is going on here and how I might go about showing a dialog from the other AppDomain's DLL and have both the caller and the callee still respond and paint properly?

推荐答案

尝试将appdomain1的主窗体的BeginInvoke与显示来自appdomain2的窗体的委托一起使用.因此在伪代码中:

Try using appdomain1's main form's BeginInvoke with a delegate that displays the form from appdomain2. So in Pseudocode:

Appdomain1:
    AppDomain2.DoSomething(myMainForm);

AppDomain2:
    DoSomething(Form parent)
    {
        Form foolishForm = new Form();
        parent.BeginInvoke(new Action( delegate { foolishForm.Show(); } ));
    }

该代码可能并不完美,但是它演示了这一概念.

The code may not be perfect, but it demonstrates the concept.

顺便说一句,如果由于远程处理而在传递表单方面遇到问题,您可以:

By the way, if you are having problems passing forms around because of remoting you can:

public class Container<T> : MarshalByRefObject
{
    private T _value;
    public T Value { get { return _value; } set { _value = value; } }

    public Container() { }
    public Container(T value) { Value = value; }

    public static implicit operator T(Container<T> container)
    {
        return container.Value;
    }
}

其中将包含您扔向它的物体.

That will contain object you throw at it.

这篇关于消息泵和AppDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆