c# - Wrong Thread Exception when changing an object's property from an event delegate -


i have playbackmodel class instantiate in mainpage of app, so:

if(playbackmodel.current == null) new playbackmodel(); 

i use playbackmodel storing references objects use in app. works viewmodel majority of pages. defines these properties, among others:

public mediaplayer player { get; set; } public mediaplaybackstate playbackstate { get; set; } public repeatmode repeatmode { get; set; } public mediaplaybacklist queue { get; set; } [alsonotifyfor("currentindex", "currenttrack")] public mediaplaybackitem currentitem { get; set; } public observablecollection<playbackitemviewmodel> tracks { get; set; } public int currentindex {     {         return (int)queue.currentitemindex;     } set {         system.diagnostics.debug.writeline("currentindex setter called! value: "+value);         if (value >= 0 && queue.items.count() > value && value != queue.currentitemindex)             queue.moveto((uint)value);     } } public playbackitemviewmodel currenttrack { { if (currentindex == -1) return null; if (tracks.count() > currentindex) return tracks[currentindex]; else return null; } } private timespan _currentposition; public timespan currentposition { { return _currentposition; } set { _currentposition = value; player.playbacksession.position = _currentposition; } } public timespan currentduration { get; set; } public library library { get; set; } public lastfmclient lastfm { get; set; } 

in inspector of playbackmodel i'm subscribing events keep track of what's going on media player , queue, can later display in ui. example:

player.playbacksession.playbackstatechanged += (o, ea) => {         playbackstate = o.playbackstate; }; 

but won't work. throws wrong_thread exception. it's same these other events. tried using await coreapplication.mainview.corewindow.dispatcher.runasync() method run code on ui thread understand it, won't work when app in background playback mode. how can other way works supports background playback?

for decided use itemchanged event:

queue.currentitemchanged += async (o, ea) => {     await coreapplication.mainview.corewindow.dispatcher.runasync(coredispatcherpriority.normal, () =>     {         var pos = currentposition.duration();         var bg = app.ref.isbackground;         task<scrobbleresponse> res = null;         currentposition = timespan.zero;         currentitem = o.currentitem;         if (ea.olditem != null)         {             var vm = ea.olditem.source.customproperties["vm"] playbackitemviewmodel;             if (pos.totalseconds >= ea.olditem.source.duration.value.totalseconds * 0.9)             {                 res = lastfm.scrobbler.scrobbleasync(new scrobble(vm.artist, vm.album, vm.title, datetimeoffset.now));             }         }         if (currentitem != null)             currentduration = o.currentitem.source.duration.value;         else currentduration = timespan.zero;     }); }; 

is there other way it?

thanks!

the code in dispatcher runs correctly. it's if statement that's not being called due conditions not updating in background. bad.


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -