c# - Cross-Thread Unauthorized AccessException MainViewModel->async LoadData() -



using windows phone 8, c# , restsharp.
getting information error raddiagnostics:

[type]:[unauthorizedaccessexception] [exceptionmessage]:[invalid cross-thread access.] [stacktrace]:[    @ ms.internal.xcpimports.checkthread()    @ system.windows.dependencyobject.setvalueinternal(dependencyproperty dp, object value, boolean allowreadonlyset)    @ system.windows.dependencyobject.setvalue(dependencyproperty property, boolean b)    @ microsoft.phone.shell.progressindicator.set_isindeterminate(boolean value)    @ rwj.misc.globalloading.notifyvaluechanged()    @ rwj.misc.globalloading.set_isloading(boolean value)    @ rwj.viewmodels.mainviewmodel.executeasync[t](restrequest request, string host)    @ rwj.viewmodels.mainviewmodel.<loaddata>d__a.movenext() --- end of stack trace previous location exception thrown ---    @ system.runtime.compilerservices.asyncmethodbuildercore.<throwasync>b__0(object state)] 


sample code:

public async void loaddata() {    // authenticate application    string authentication = encodeto64(consumerkey + ":" + consumersecret);    restclient twitterclient = new restclient("https://api.twitter.com");    irestresponse resp = null;    oauthtoken token = null;    restrequest tweetreq = new restrequest("oauth2/token", method.post);    tweetreq.addheader("authorization", "basic " + authentication);    tweetreq.addheader("host", "api.twitter.com");    tweetreq.addheader("user-agent", "[app name]");    tweetreq.addheader("content-type", "application/x-www-form-urlencoded;charset=utf8");    tweetreq.addparameter("grant_type", "client_credentials");    try    {       resp = await twitterclient.getresponseasync(tweetreq);       token = newtonsoft.json.jsonconvert.deserializeobject<oauthtoken>(resp.content);    }    catch (exception ex)    {       // error handling    }     // fetch tweets    twitterclient.baseurl = tapi;    tweetreq = new restrequest("search/tweets.json", method.get);    tweetreq.addheader("authorization", "bearer " + token.access_token);    tweetreq.addheader("accept-encoding", "gzip");    tweetreq.addparameter("q", "from:twitterapi exclude:retweets exclude:replies");    tweetreq.addparameter("count", "10");     string[] _createdat = new string[10];    string[] _text = new string[10];    string[] _link = new string[10];    string[] _image = new string[10];     peep res = await executeasync<peep>(tweetreq, tapi).configureawait(false);    status[] statuses = res.statuses;    int = 0;    foreach (status tweet in statuses)    {       _createdat[i] = tweet.created_at;       _text[i] = tweet.text;       if (tweet.entities.urls.length > 0) { _link[i] = tweet.entities.urls[0].expanded_url; } else { _link[i] = ""; }       if (tweet.entities.media != null) { _image[i] = tweet.entities.media[0].media_url; } else { _image[i] = ""; }       i++;    }     (int x = 0; x < i; x++)    {       if (_link[x].contains("youtube.com") || _link[x].contains("youtu.be"))       {          regex yt = new regex(@"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-za-z0-9-_]+)");          match youtube = yt.match(_link[x]);          if (youtube.success)          {             _linktype[x] = "youtube";             _link[x] = youtube.groups[1].value;          }          else { _linktype[x] = "url"; }       }       else if (_link[x].contains("instagram.com") || _link[x].contains("instagr.am"))       {          restrequest ireq = new restrequest();          ireq.addparameter("url", _link[x]);          instagram inst = await executeasync<instagram>(ireq, instaapi).configureawait(false);          _linktype[x] = "url";          _image[x] = inst.url;       }       else if (_link[x].contains("http://t.co/"))       {          //do nothing, image url should loaded in _image[x]       }       uithread.invoke(() =>       {          // cross thread operation not valid          this.twitter.add(new tweetviewmodel() { date = _createdat[x], tweet = _text[x], link = _link[x], linktype = _linktype[x], image = _image[x] });       });    } } 

this code works flawless until point it's supposed add item collection.
that's exception thrown.

the uithread class here: unauthorizedaccessexception: invalid cross-thread access in silverlight application (xaml/c#)

since using await, don't need uithread @ all. remove configureawait(false) calls.


Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -