I'm going to implement MVC structure of delegate same like in Objective-C. Basically my functionality is like Bluetooth device return me signal i grab it in C++ Callback and that signal message i going to forward to callback of C# lambda function. I got following error,
Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on.
My C# code Form1.cs,
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
delegate void ProgressCallback(char theValue);
private void button1_Click(object sender, EventArgs e)
{
if (NclWrapper.isNclInitialized())
{
NclWrapper.callNclDiscovery();
label1.Text = NclWrapper.getLedPattern();
ProgressCallback callback = (theValue) =>
{
//Crash is here when i going to assign value to lable.
this.label1.Text = theValue.ToString();
Console.WriteLine("Progress = {0}", theValue);
};
NclWrapper.DoWork(callback);
}
}
}
My C++ DLL wrapper in C#
[DllImport("My.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto)]
public static extern void DoWork([MarshalAs(UnmanagedType.FunctionPtr)] ProgressCallback callbackPointer);
My C++ .cpp class delegate assignment,
ProgressCallback myDelegateObject;
__declspec(dllexport) void DoWork(ProgressCallback progressCallback)
{
if (progressCallback)
{
myDelegateObject = progressCallback;
}
}
My C++ sending callback message
void callback(MyEvent event, void* userData){
wchar_t ch[6];
ULONG ulSize;
switch(event.type){
/// [callbackInit]
case MY_EVENT_INIT:
if(event.init.success) gMyInitialized=true;
else exit(-1);
prevEvent = event.type;
break;
/// [callbackAgreement]
case My_EVENT_AGREEMENT:
gHandle=event.agreement.myHandle;
for(unsigned i=0; i<My_AGREEMENT_PATTERNS; ++i){
for(unsigned j=0; j<My_LEDS; ++j){
if(event.agreement.leds[i][j]){
ch[j]='1';
}else{
ch[j]='0';
}
}
std::cout<<"\n";
}
ch[5]='\0';
ulSize = (wcslen(ch) * sizeof(wchar_t)) + sizeof(wchar_t);
ledPattern = NULL;
ledPattern = (wchar_t*)::CoTaskMemAlloc(ulSize);
wcscpy(ledPattern, ch);
prevEvent = event.type;
//My delegate callback is here
myDelegateObject('D');
break;
default: break;
}
}
where i'm wrong?