C ++ API Dll中的事件 [英] Event in C++ API Dll
问题描述
我有一个c ++ dll,可从WinSNMP获取SNMP陷阱,然后在我的c#代码(c#中的委托)中调用函数.
一切正常.
我的问题是我希望C ++ dll每次在新线程中调用我的c#委托.有什么方法可以做到而无需在c ++中调用CreateThread.我之所以这样问,是因为我认为要购买一个调用CreateThread的程序,就必须跟踪线程的句柄然后将其关闭(这是太多的工作)
我需要类似异步事件的东西!有可能吗?
Hi,
I have a c++ dll which get SNMP traps from WinSNMP and then call a function in my c# code(a delegate in c#).
Every thing works fine.
My problem is I want the c++ dll call my c# delegate every time in a new thread. Is there any way to do that without calling CreateThread in c++. I ask this because as I think buy calling CreateThread I have to track the handle of thread and then close it ( and it is too much work)
I need something like async event! Is it possible?
typedef void (__stdcall *callback_StateChangeReceived)(char* Name, char* Text , char* DateTime);
callback_StateChangeReceived CallbackStateChangeReceived;
// I first call this function from C# to set my callback function
bool __stdcall SetCallbackStateChangeReceived(callback_StateChangeReceived Callback)
{
CallbackStateChangeReceived =Callback;
return true;
}
//And then I call the callback in a appropriate function, something like this:
while(SnmpMgrGetTrapEx(&enterprise,&AgentIPAddress, &IPAddress, &genericTrap,&specificTrap,&community, &timeStamp, &variableBindings))
{
// Some Code here
CallbackStateChangeReceived (Name,Text,sDateTime);
// I do not want this function to be blocked.
}
每当我调用CallbackStateChangeReceived时,我都需要它,它会立即返回并且不会被我的C#代码阻止. (我不想更改c#代码.)
谢谢.
I need when ever I call CallbackStateChangeReceived, It returns immediately and not to be blocked by my c# code. (I do not want to change the c# code.)
Thanks.
推荐答案
您无需跟踪使用 ^ ] API :仅在必须获取退出代码时,才需要这样做b>该线程.
然后,您可以简单地从 C ++ 代码创建一个新线程,并立即在返回的 handle 上调用CloseHandle
(这样做不会终止新创建的线程) .
You do not need to track the handle of a thread created with the CreateThread Function (Windows)[^] API: you need to do it only if you have to get the exit code of that thread.
Then you can simply create a new thread from your C++ code, and callCloseHandle
on the returned handle immediately (doing this doesn''t abort the newly created thread).
这篇关于C ++ API Dll中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!