简体   繁体   English

C#应用程序中的多个VB6公寓

[英]Multiple VB6 apartments in C# application

We have very old legacy vb6 application, that has one global object that serves as Application Core, that stores different application settings, invokes database operations and so on. 我们有一个非常古老的传统vb6应用程序,它有一个作为Application Core的全局对象,它存储不同的应用程序设置,调用数据库操作等等。 Multiple modules with different progid use this global object and have no problem with it due to single-thread apartment. 具有不同progid的多个模块使用此全局对象,并且由于单线程单元而没有问题。

Not long ago new WPF application was created, that provide us transition from vb6, however it is still limited by vb6 legacy due to some architectural mistakes. 不久前创建了新的WPF应用程序,它为我们提供了从vb6的过渡,但由于一些架构错误,它仍然受到vb6遗留的限制。 It is able to connect to only one database per app instance. 每个应用程序实例只能连接一个数据库。 It hold static instance of vb6 global object in wrapper class, that serves as bridge to reach legacy functionality. 它在包装类中保存vb6全局对象的静态实例,用作实现遗留功能的桥梁。

Now, we are developing new application that should not be limited by old legacy code, in particular it new application should be able to connect to several databases at once, but there is a catch: vb6 code limited to single database, so there should be several instances of vb6 global objects, one per each database. 现在,我们正在开发不应受旧旧代码限制的新应用程序,特别是新应用程序应该能够同时连接到多个数据库,但有一个问题:vb6代码仅限于单个数据库,所以应该有vb6全局对象的几个实例,每个数据库一个。

So the question is: is it possible, and if it is, how is it possible to use several separated intstances of global vb6 objects in same C# application? 所以问题是:它是否可能,如果可能的话,如何在同一个C#应用程序中使用几个独立的全局vb6对象实例?

I presume that each instance of such object should live in it's own STA -Thread, but i don't know how to create such threads, that are kept alive for entire application runtime and that have assotiated wrappers, containing instances of global vb6 objects and supporting invocation of some functions from GUI thread (and how to organize such cross-thread communication, there is no thread.invoke(...)). 我假设这样的对象的每个实例都应该存在于它自己的STA -Thread中,但是我不知道如何创建这样的线程,这些线程对于整个应用程序运行时保持活动状态并且具有相关的包装器,包含全局vb6对象的实例和支持从GUI线程调用一些函数(以及如何组织这样的跨线程通信,没有thread.invoke(...))。 I thinked about using wpf dispatcherization model ( wrapper class is DispatcherObject, each instance has it's own Dispatcher with it's own STA-Thread), but i cannot see how to implement such thing. 我想到使用wpf调度程序模型(包装器类是DispatcherObject,每个实例都有它自己的Dispatcher和它自己的STA-Thread),但我看不出如何实现这样的事情。 Also I think it could be imlemented by loading each instance of wrapper class (static) in different AppDomains, but i don't know if it resolves STA problem for COM. 另外我认为可以通过在不同的AppDomain中加载包装类(静态)的每个实例来实现它,但我不知道它是否解决了COM的STA问题。

It is possible, using what is referred to as unmanaged code. 使用所谓的非托管代码是可能的。 Check out this: http://www.codeproject.com/Articles/154144/Using-Unmanaged-VB6-Code-in-NET 看看这个: http//www.codeproject.com/Articles/154144/Using-Unmanaged-VB6-Code-in-NET

You might get away with running one instance of the global object per AppDomain . 您可能会因为每个AppDomain运行一个全局对象实例而逃脱。

To be safe, you should run them each in its own process, since that's the assumption they were created with. 为了安全起见,您应该在每个进程中运行它们,因为这是他们创建的假设。 I guarantee you that horrible things can happen when you violate the assumptions of ancient code - especially when you introduce them to things which simply did not exist when they were written. 我向你保证,当你违反古代代码的假设时会发生可怕的事情 - 特别是当你把它们介绍给那些在编写时根本不存在的东西时。

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

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