winforms - Awesomium in C# Exception Fix? -


i've been working on c# winform project has part in when user keys "enter" button, backgroundworker set new uri in source property of awesomium instance. however, i'm encountering aweinvalidoperationexception ("the calling thread cannot access object because different thread owns it.").

here's sample code of part of work:

private void txt_to_keyup(object sender, keyeventargs e) {    if (e.keycode == keys.enter)    {    backgroundworker.runworkerasync();    } }  private void backgroundworker_dowork(object sender, system.componentmodel.doworkeventargs e) {   //get values xml file , (not included here anymore it's long)   awesomiuminstance.source = new uri("http://maps.google.com?saddr=" +                              txt_from.text.replace(" ", "+") + "&daddr=" +                                  txt_to.text.replace(" ", "+") + flag +                              "&view=map&hl=en&doflg=ptk"); } 


aweinvalidoperationexception happens @ line 12

awesomiuminstance.source = new uri("http://maps.google.com?saddr=" +                            txt_from.text.replace(" ", "+") + "&daddr=" +                            txt_to.text.replace(" ", "+") + flag +                            "&view=map&hl=en&doflg=ptk"); 


here's screenshot of exception:

aweinvalidoperationexception screenshot

what fix/solution exception?

you need invoke code on ui thread, since not using wpf don't have dispatcher, can try invoke or begininvoke following

        this.begininvoke((action)(() =>         {             awesomiuminstance.source = new uri("http://maps.google.com?saddr=" +                             txt_from.text.replace(" ", "+") + "&daddr=" +                             txt_to.text.replace(" ", "+") + flag +                             "&view=map&hl=en&doflg=ptk");         })); 

invoke causes action execute on thread created control's window handle. begininvoke asynchronous version of invoke means thread not block caller unlike synchronous call blocking.

if doesn't work can use synchronizationcontext

edit: post asynchronous

        synchronizationcontext.current.post(_ =>         {             awesomiuminstance.source = new uri("http://maps.google.com?saddr=" +                             txt_from.text.replace(" ", "+") + "&daddr=" +                             txt_to.text.replace(" ", "+") + flag +                             "&view=map&hl=en&doflg=ptk");         }, null); 

synchronizationcontext assists in executing code on ui thread necessary, delegates passed send or post method invoked in location. (post non-blocking of send.) more detailed explanation please visit this link

if synchronizationcontext.current null check this link when , when not null


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -