Click here to Skip to main content
15,884,064 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
}

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.
Posted

1 solution

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 call CloseHandle on the returned handle immediately (doing this doesn't abort the newly created thread).
 
Share this answer
 
Comments
aidin Tajadod 23-Sep-10 12:41pm    
Thanks Sauro,
when I am calling closehandle I have to have the handles of the thread created before. Is it correct? and this dll is using by the windows service. by not calling closehandle for every thread what happend? Is the refrences of that thread released after the thread finished? (sorry I am beginner in c++)
Sauro Viti 23-Sep-10 12:54pm    
The CloseHandle function wants as parameter just one handle, the one that you want to close. If you don't close the handles, the created threads remain allocated also after their termination, and you risk resource and memory allocation problems (the number of thread a process can create is limited by the available virtual memory). Then you can write something like this inside your while loop:

HANDLE hThread = CreateThread(NULL, 0, functionAddress, parameters, 0, NULL);
if (hThread != NULL) CloseHandle(hThread);
else { /* Thread creation error! */ }
aidin Tajadod 23-Sep-10 13:02pm    
Thanks so much, you are right. I found this also in MSDN: Closing a thread handle does not terminate the associated thread. To remove a thread object, you must terminate the thread, then close all handles to the thread.
Thanks again.
aidin Tajadod 23-Sep-10 13:02pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900