【问题标题】:send status updates from function in C++ to C#将状态更新从 C++ 中的函数发送到 C#
【发布时间】:2012-10-19 08:02:17
【问题描述】:

我的 C++ dll 中有一个非常大的函数,它执行很多任务。 我们从 c# 包装器中调用它,完成 c++ 函数大约需要 20 秒。 我想改变我们运行它的方式。 我的想法是 1.调用c++函数async和 2. 每次使用 C++ 函数完成任务时,我想向 C# 函数发送“task1 已完成”消息并将其显示给用户,以便他们知道后台发生了什么。

任何想法如何执行这个?我查了几个例子,但感到困惑。我想知道是否有人这样做过。寻找一些指针。

EX:C++ 代码

int  CppLibrary::ExecuteWorkflow( param1,param2, param3,param4,param5)
{
task1;
task2;
task3;
task4;
task5;

}

calling the C++ function from C# wrapper:

[DllImport(_dllLocation)]
public static extern int ExecuteWorkflow( param1,param2, param3,param4,param5);

【问题讨论】:

    标签: c# c++ events asynchronous delegates


    【解决方案1】:

    这是 P/Invoke C++ 函数的包装类。希望能帮到你。

    class CSUnmangedTestClass : IDisposable
    {
        #region P/Invokes
    
        [DllImport(@"E:\VS2012Tests\test\Debug\DllImport.dll", EntryPoint="#1")]
        private static extern IntPtr Foo_Create();
    
        [DllImport(@"E:\VS2012Tests\test\Debug\DllImport.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern int Foo_Bar(IntPtr pFoo);
    
        [DllImport(@"E:\VS2012Tests\test\Debug\DllImport.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void Foo_Delete(IntPtr pFoo);
    
        #endregion
    
        #region Members
        // variable to hold the C++ class's this pointer
        private IntPtr m_pNativeObject;
        #endregion
    
        public CSUnmangedTestClass()
        {
            this.m_pNativeObject = Foo_Create();
        }
    
        public void Dispose()
        {
            Dispose(true);
        }
    
        protected virtual void Dispose(bool bDisposing)
        {
            if (this.m_pNativeObject != IntPtr.Zero)
            {
                Foo_Delete(m_pNativeObject);
                this.m_pNativeObject = IntPtr.Zero;
            }
            if (bDisposing)
            {
                // No need to call the finalizer since we've now cleaned up the unmanged memory
                GC.SuppressFinalize(this);
            }
        }
    
        ~CSUnmangedTestClass()
        {
            Dispose(false);
        }
    
        #region Wrapper methods
    
        public int Bar()
        {
            return Foo_Bar(m_pNativeObject);
        }
    
        #endregion
    }
    

    【讨论】:

      【解决方案2】:
      1. 使用类似 C 的名称导出 C++ 函数(export "C" __declspec(dllexport))
      2. 使用 DllImport 为您的库调用创建一个 DllImport。
      3. 创建一个线程并使用您的回调逻辑(即 Task.Run 与委托)调用导入。

      【讨论】:

      • 我认为你搞反了。
      • 嗨。我正在执行您建议的前两个步骤。你能详细说明第三步吗?我知道如何在 c# 中使用线程。但我想显示从 C++ 发送的状态消息。我该怎么做?谢谢。
      【解决方案3】:

      您可以在 C# 中使用委托来调用您的 C++ 包装器,然后根据您的情况使用“invoke”或“beginInvoke”。

      Dispatcher.BeginInvoke Method

      【讨论】:

      • 谢谢,您能详细说明一下吗?你建议我在我的 c# 包装器中使用委托。但是我在哪里使用invoke或begininvoke?我想将状态消息从我的 c++ 函数发送到 c# 包装器。那么如何从 C++ 发送它以及如何在 C# 中捕获消息?请理解我以前从未使用过 c++ 或事件委托。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2011-05-02
      • 2013-01-08
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      相关资源
      最近更新 更多