将状态更新从C 中的功能发送到C#
send status updates from function in C++ to C#
我在C DLL中具有很大的功能,可以执行许多任务。我们从C#包装器称呼它,并且C 功能要完成约20秒。我想改变我们运行它的方式。我的想法是1.调用C 函数异步和2.每次完成C 功能的任务完成时,我想将" Task1已完成"消息发送到C#功能并将其显示给用户,以便他们知道后台发生了什么。
有什么想法如何执行?我抬头看了几个例子,但感到困惑。我想知道是否有人这样做。寻找一些指针。
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 包装器,然后根据您的情况使用"调用"或" beginInvoke"。
dispatcher.begininvoke方法
- 使用c like名称(导出" c" __declspec(dllexport)) 导出您的C 功能
- 使用dllimport为您的库呼叫创建dllimport。
- 创建一个线程并使用您的回调逻辑调用导入(即任务。与委托)。
这是P/Indoke C 函数的包装类别。希望可以帮助您。
class CSUnmangedTestClass : IDisposable
{
#region P/Invokes
[DllImport(@"E:VS2012TeststestDebugDllImport.dll", EntryPoint="#1")]
private static extern IntPtr Foo_Create();
[DllImport(@"E:VS2012TeststestDebugDllImport.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int Foo_Bar(IntPtr pFoo);
[DllImport(@"E:VS2012TeststestDebugDllImport.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
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 返回状态后,功能不会结束
- 将状态传递给功能指针
- 当我使用该功能时,"错误 ld 返回 1 个退出状态"boost::log::add_file_log()"
- 如何实现更快的状态检查功能?蟒
- 将状态更新从C 中的功能发送到C#
- 主要功能和状态之间的通信
- 状态功能.如何通过变量给出参数
- 在开关状态情况下,模板功能内部的不同类型的分配