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

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

问题描述

我有一个 C# (FFx 3.5) 应用程序,它将 DLL 作为插件加载.这些插件加载在单独的 AppDomains 中(有很多很好的理由,而且这个架构不能改变).这一切都很好.

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 的 Form.对话框大纲出现但没有绘制,它不响应鼠标事件.我认为这是因为 DLL 位于单独的 AppDomain 中,并且应用程序的消息泵以某种方式无法将消息发送到新表单.

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.

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

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