Unable to find out reason of error Cross-thread operation not valid from C++ callback to C# -
i'm going implement mvc structure of delegate same in objective-c. functionality bluetooth device return me signal grab in c++ callback , signal message going forward callback of c# lambda function. got following error,
cross-thread operation not valid: control 'label1' accessed thread other thread 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 here when going assign value 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 here mydelegateobject('d'); break; default: break; } }
where i'm wrong?
progresscallback callback = (thevalue) => { label1.invoke((methodinvoker)delegate { label1.text = thevalue.tostring(); }); };
this code stuff update label text.
Comments
Post a Comment