dimanche 8 mars 2015

Allow native C++ components to raise CLR Asynchronous events


I need in my Native code to raise an asynchronous event to C#,I have used as bridge CLI ,actually I have gone though sending a pointer to function from C++/CLI but it did not work properly? I need to know:1-What is wrong? 2-how can make it asynchronous event to not block the processing? I need in this example to raise an event each time I reach 1000,here is my code Count.h



typedef void pointertofunc(bool IsOverFlowOccured);
typedef pointertofunc *pointertofuncdelegate;
class count
{

public:
void startCounting(pointertofuncdelegate);
count();
~count();
};


Count.cpp



void count::startCounting(pointertofuncdelegate)
{
for (int i = 0; i < 100000; i++)
{
//printf("%d \n", i);
if (i == 1000)
{
pointertofuncdelegate(true);
printf("%d \n", i);
i = 0;
}
}
}


CLR(.h file)



public delegate void manageddelegate(bool isworkflowoccured);
ref class CounterRaiseAsynchronousEvent
{
public:
event manageddelegate^ managedEventHandler;
CounterRaiseAsynchronousEvent();
void initialize();
void raiseEvent(bool eoverFlow);

private:
count* wrapperObject;
};


CLR(.cpp file)



void CounterRaiseAsynchronousEvent::initialize()
{
//Create a new delegate and point it to the member function
manageddelegate^ prDel = gcnew manageddelegate(this, &CounterRaiseAsynchronousEvent::raiseEvent);
GCHandle gch = GCHandle::Alloc(prDel);
//Convert the delegate to a function pointer
IntPtr ip = Marshal::GetFunctionPointerForDelegate(prDel);
//... and cast it to the appropriate type
pointertofuncdelegate fp = static_cast<pointertofuncdelegate>(ip.ToPointer());
//set the native function pointer on the native class
wrapperObject->startCounting(fp);
}
void CounterRaiseAsynchronousEvent::raiseEvent(bool isOverflowOccurred)
{
//Do any thing
}



Aucun commentaire:

Enregistrer un commentaire