C# 为什么调用托管c++;类不';Don’不要等它结束了?

C# 为什么调用托管c++;类不';Don’不要等它结束了?,c#,c++,command-line-interface,clr,managed,C#,C++,Command Line Interface,Clr,Managed,假设我们遇到这样的情况: //Idl文件实现 [ object, uuid(44E43325-D296-4875-8473-88E1076BA98A), helpstring("INativeCpp Interface"), pointer_default(unique) ] interface INativeCpp : IDispatch { [id(1)] HRESULT ExecuteCommand([in] long CommandID); } library

假设我们遇到这样的情况:

//Idl文件实现

[
  object,
  uuid(44E43325-D296-4875-8473-88E1076BA98A),
  helpstring("INativeCpp Interface"),
  pointer_default(unique)
]

interface INativeCpp : IDispatch
{
   [id(1)] HRESULT ExecuteCommand([in] long CommandID);
}

library NativeCppLib
{
    importlib("stdole32.tlb");
    importlib("stdole2.tlb");

    [
       uuid(98D53837-D12A-439B-AA1B-2B838EEF30CF),
       helpstring("NativeCppClass")
    ]
    coclass NativeCpp
    {
       [default] interface INativeCpp;
    };
}
//天然C++

//I had to define this interface with the same method as INativeCpp com interface as I was having some access dilemma. So, i defined this interface at the common location which NativeCpp class and ManagedCpp class both can access.
class NativeCppInterface
{
 public:
    NativeCppInterface();
    virtual ~NativeCppInterface();
    virtual HRESULT ExecuteCommand(long CommandID) = 0;
}

enum enCommands
{
     Save = 1,
     Refresh = 2
}

class ATL_NO_VTABLE NativeCpp:
  public CComObjectRootEx<CComSingleThreadModel>,
  public CComCoClass<NativeCpp, &CLSID_NativeCpp>,
  public IDispatchImpl<INativeCpp, &IID_INativeCpp, &LIBID_NativeCppLib>,
  public NativeCppInterface
{
   STDMETHODIMP ExecuteCommand(long CommandID)   //Takes around 2 minutes.
   {
      switch (enCommands(CommandID))
      {
         case enCommands::Save:
           if (!!_pSaveHandler)
           {
              _pSaveHandler->OnExecute();  //Executes some sequential methods and then launches one window. And then proceeds.
           }
           break;
         case enCommands::Refresh:
            if (!!_pPropertiesHandler)
            {
                    GetObjectDeatilsList(4);    //Executes some sequential methods. Nothing special.
            }
            break;
         default:
            break;
       }
       return S_OK;
    }
}
//C#项目

现在,当我调用
DoOperation()
时,调用转到NativeCpp的
ExecuteCommand()
,但在
ExecuteCommand()
完成之前,C#代码执行转到执行
int nextline=5。它不会等待
ExecuteCommand()
完成。为什么呢?我该怎么做才能让它等待呢

注意:我已尝试使用System.Windows.Application.Current.Dispatcher.Invoke()
。这就是诀窍。但是,如果与ManagedCpp的通信频繁,则有时Dispatcher会收到太多调用,并挂起。所以,我想知道为什么首先异步启动对ManagedCpp的调用


我还想从
ExecuteCommand()
方法启动一些UI窗口,所以我真的想在线程C#代码正在运行时调用这个方法。

ExecuteCommand()函数包含什么?如果看不到“ExecuteCommand()”中的内容,就无法判断。从你所展示的,没有什么是asynchronous@WBuck我已经更新了代码。ExecuteCommand()只执行顺序方法。这没什么特别的。另外,NativeCpp类是一个com实现的类,其方法在idl文件中定义。我不知道此信息是否重要。我正在尝试在调用事件订阅方方法时调用ExecuteCommand。因此,该线程是MTA线程。这可能与该方法异步执行的原因有关!ExecuteCommand()函数包含什么?如果看不到“ExecuteCommand()”中的内容,就无法判断。从你所展示的,没有什么是asynchronous@WBuck我已经更新了代码。ExecuteCommand()只执行顺序方法。这没什么特别的。另外,NativeCpp类是一个com实现的类,其方法在idl文件中定义。我不知道此信息是否重要。我正在尝试在调用事件订阅方方法时调用ExecuteCommand。因此,该线程是MTA线程。这可能与该方法异步执行的原因有关!
public ref class ManagedCpp : IWrapper
{
  NativeCppInterface* native;
  ManagedCpp(NativeCppInterface* ptr)
  {
    native = ptr
  }
  void ExecuteCommand(int64_t CommandID)
  {
     native->ExecuteCommand((long)CommandID);
  }
}
public interface IWrapper
{
  void ExecuteCommand(long CommandID);
}

public class MainClass
{
  IWrapper _wrapper;

  MainClass(IWrapper wrapper)
  {
    _wrapper = wrapper;
  }

  void DoOperation()
  {
    _wrapper.ExecuteCommand(1);
    int nextline = 5;
  }
}