简体   繁体   English

C#Singleton暴露于MS Word Interop加载项

[英]C# Singleton exposed to MS Word Interop Add-in

I've been having an issue and only recently pinpointed the following as being the cause (was troubleshooting a symptom until now). 我一直遇到问题,直到最近才查明了以下原因(目前为止一直在排除症状)。 Basically, I have a Solution that has a WPF application project (the startup), a bunch of assemblies (Model, ModelView, etc), and a Word VSTO Add-in. 基本上,我有一个具有WPF应用程序项目(启动),一堆程序集(Model,ModelView等)和Word VSTO加载项的解决方案。 The ViewModel and the Word Add-in both compose one of the Models, which for this stage of development, I have made a Singleton. ViewModel和Word加载项都组成了一个模型,在此开发阶段,我制作了一个Singleton。

The Viewmodel instantiates the model in its constructor using Viewmodel使用以下方法在其构造函数中实例化模型

ModelSingleton mod =  ModelSingleton.Instance;

The addin does the same thing on ThisAddIn_Startup() method. 插件在ThisAddIn_Startup()方法上执行相同的操作。

Model code: 型号代码:

private static volatile ModelSingleton instance;
private static object syncRoot = new Object();

private ModelSingleton()
{
}

public static ModelSingleton Instance
{
    get
    {
        lock (syncRoot)
        {
            if (instance == null)
            {
                 instance = new ModelSingleton();
            }
        }
        return instance;
    }
}

I want the Word Addin to capture highlighted text and pass it to my model, which then generates its own event that the Viewmodel is subbed to. 我希望Word Addin捕获突出显示的文本并将其传递给我的模型,该模型然后生成自己的事件,将Viewmodel嵌入该事件。

My issue is that both the addin and the viewmodel are creating separate instances of what is supposed to be a Singleton. 我的问题是插件和视图模型都在创建应该是Singleton的单独实例。 The highlighted text is passed to one Singleton but no event is generated because the ViewModel subscribed to the event in another instance. 高亮显示的文本将传递给一个Singleton,但不会生成任何事件,因为ViewModel在另一个实例中订阅了该事件。

I'm a newb to Interop and COM; 我是Interop和COM的新手。 I would expect a singleton to only be instantiated once. 我希望单例仅实例化一次。 Are there two separate runtimes or something going on here (apparently, the VSTO and the WPF are both executables which means they cannot directly share data)? 这里是否有两个单独的运行时或正在运行的东西(显然,VSTO和WPF都是可执行文件,这意味着它们无法直接共享数据)? How do I make the Singleton a true Singleton? 如何使Singleton成为真正的Singleton? And if that's impossible, how would you pass highlighted text (and metadata) from MS Word to your proprietary app? 如果不可能,那么如何将突出显示的文本(和元数据)从MS Word传递到专有应用程序?

I think you are trying to do this: 我认为您正在尝试这样做:

public static ModelSingleton Instance
{
    private static ModelSingleton instance = null;
    private static readonly object syncRoot = new Object();

    private ModelSingleton()
    {
    }
    get
    {
        lock (syncRoot)
        {
            if (instance == null)
            {
                 instance = new ModelSingleton();
            }
        }
    return instance;
    }
}

this will give you thread safety without any deadlocks 这将为您提供线程安全性而没有任何死锁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM