简体   繁体   English

C#强制实例化外部exe

[英]C# Force instantiation of external exe

I need to call a third party c++ exe (that presents an OLE automation interface) from my app. 我需要从我的应用程序调用第三方c ++ exe(提供OLE自动化接口)。 I'd like to be able to run multiple instances of my app concurrently. 我希望能够同时运行我的应用程序的多个实例。 The third party app I call has the option of running against different databases. 我调用的第三方应用程序可以选择针对不同的数据库运行。

If I run multiple instances of my app (which calls the third party app) against the same database, all is well. 如果我在同一个数据库上运行我的应用程序(称为第三方应用程序)的多个实例,则一切正常。

If I try and run one instance against one database and another instance against another database I get the following error: 如果我尝试对一个数据库运行一个实例,对另一个数据库运行另一个实例,则会收到以下错误:

The server threw an exception. 服务器引发异常。 (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) (来自HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))

I suspect that it is due to the second instance not creating a new instance of the third party app but using the same instance as the first copy of my app. 我怀疑这是由于第二个实例没有创建第三方应用程序的新实例,而是使用了与我的应用程序的第一个副本相同的实例。

Does this sound likely? 听起来可能吗? I suspect so as if I look in Task Manager I can only see one copy of the third party app running in background processes, but two copies of my app running in Apps. 我怀疑好像在任务管理器中查看一样,我只能看到在后台进程中运行的第三方应用程序的一个副本,但是在Apps中运行的我的应用程序的两个副本。

So,how do I force my app to create a second instance of the third party app? 那么,如何强制我的应用创建第三方应用的第二个实例?

Code: 码:

 Type tpType = Type.GetTypeFromProgID("thirdPty.Application");
 dynamic comObject = Activator.CreateInstance(tpType);

 try
 {
            bool success = comObject.LoadDatabase(dbPath);

            if (success)
            {
                var newTp = comObject.Open(inputFile);

                newTp.Run(runType);

                while (newTp.IsBusy)
                {
                    // wait for process to finish
                    Thread.Sleep(500);
                }

                newAb.Export(outputFolder + "\\output" + " " + DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".csv");

                newAb.Close();
            }
 }

I don't think you can. 我认为你不能。 When you call Activator.CreateInstance , it's up to the (D)COM server how it instantiates it's Application COM objects. 调用Activator.CreateInstance由(D)COM服务器决定如何实例化其Application COM对象。

Apparently, this one is registered with REGCLS_MULTIPLEUSE , meaning multiple instance will be served from the same server process. 显然,这是向REGCLS_MULTIPLEUSE注册的,这意味着将从同一服务器进程提供多个实例。 One would assume that if that is designed behaviour of the COM server, it should also behave nicely when using different database connections for each Application object. 可以假设,如果这是COM服务器的设计行为,则在为每个Application对象使用不同的数据库连接时,它也应该表现良好。

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

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