API call back error from deferent Thread

G

Guest

I have an API, with a call back error definition. API receive reference to C#
output message function (write to text box using Invoke since I use thread).

Every thing is working fine API can write a message to my GUI textbox, but
when I access the API from another THREAD , API try to call this function
(reference to output message function), I get an exception. I understand that
this exception somehow because invoke issue, but how can I solve this
problem. In my GUI C# code I use the Invoke but I not reach the C# since the
exception in my API.

******
API definition:
******
typedef void (__stdcall * LOG_CALLBACK)(LOGCALLBACKINFO *);
LOG_CALLBACK ptrLogCallBackFunction = NULL;
******
API set function:
******
DLL_API void CALL_SetCallbackGUI (LOG_CALLBACK ptrLOG_CALLBACK);


******
C#:
******

[DllImport("CALL_DLL.dll", EntryPoint="CALL_SetCallbackGUI_NoStyleLog",
CallingConvention=CallingConvention.StdCall)]
public static extern int SetLogCallBack(DelegateLogCallBack
delegate_callback);


Set API reference to my internal C# function by:


SetLogCallBack(new DelegateLogOutputCallBack(internalSetLogCallBackHandler));

public void internalSetLogCallBackHandler(ref LOGCALLBACKINFO cInfo)
{
this.mForm.invoke_outPut(cInfo.log_text);
}

public void invoke_outPut(string outStr)
{
object [] args = new object[1];
args[0] = outStr;
InvokerFunctionString funcP = new
InvokerFunctionString(this.mForm.outPut);
this.mForm.Invoke(funcP,args);

}
 
M

Mattias Sjögren

I can't say for sure what's causing the exception. I twould be useful
to see the native and managed declaration of LOGCALLBACKINFO too.

SetLogCallBack(new DelegateLogOutputCallBack(internalSetLogCallBackHandler));

Here you should keep a reference to the callback delegate to make sure
it's valid as long as the callback is needed. Otherwise the delegate
may get garbage collected.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top