Getting the interface was configured for another thread Exception with a call to the UI thread manager?

I am working on a WinRT / Windows Store application written in C # that uses MVVM Light. I am trying to change the MVVM Light OnPropertyChanged () method so that event notifications always appear in the user interface thread, otherwise the interface elements bound to my view model properties will skip event notifications because they should occur in the user interface stream. I am currently trying to use this code:

/// <summary>
/// Event handler for the PropertyChanged event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
protected void OnPropertyChanged(string propertyName = null)
{
    var eventHandler = PropertyChanged;
    if (eventHandler != null)
    {
        // Make sure the event notification occurs on the UI thread or the INotifyPropertyChanged
        //  notification will not be seen by the consumer of this event or worse, 
        //  a "wrong thread" COM Exception will be raised if we are not on the UI thread.
        var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;

        dispatcher.RunAsync(dispatcher.CurrentPriority,
            () =>
            {
                eventHandler(this, new PropertyChangedEventArgs(propertyName));
            });
    }
}

I used this code before embedding the code in the user interface thread. However, in this case, I get the following exception:

"System.Exception" Common_WinStore.DLL

WinRT: , , marshalled .

, CoreWindow.Dispatcher , , UI, . marshaling SO, RunAsync() . , , .

Dispatcher.RunAsync() ?

. - , , WriteableBitmap . , , , . , ConfigureAwait (false) , . COM Exception, MVVM Light OnNotifyPropertyChanged() . . , , , , , .

UPDATE: AsyncEx, . . , . , , JPEG, SQLite-. , async JPEG WriteableBitmap. - , , , , .

AsyncManualResetEvent, . set JPEG bytes . async- , . , , - :

    // Create an Async manual reset event for the JPEG conversion method in the unset state.
    private AsyncManualResetEvent _asyncManResetEvent = new AsyncManualResetEvent(false);


    /// <summary>
    /// The jpeg bytes for the JPEG image for the videomark.
    /// </summary>
    private byte[] _thumbnailJpegBytes;

    public byte[] ThumbnailJpegBytes
    {
        get
        {
            return this._thumbnailJpegBytes;
        }

        set
        {
            SetProperty(ref this._thumbnailJpegBytes, value, "ThumbnailJpegBytes");

            // Release the lock so that the async call waiting to convert the JPEG bytes to
            //  a WriteableBitmap can finish up.
            _asyncManResetEvent.Set();
        }
    }

// The WriteableBitmap property.
[Ignore]
public INotifyTaskCompletion<WriteableBitmap> ThumbnailAsync
{
    get;

    private set;
}

    // The async method passed to the NotifyTaskCompletion constructor.
async private Task<WriteableBitmap> ConvertJpegBytesToThumbnail()
{
    // Wait for data to be available.
    await this._asyncManResetEvent.WaitAsync();

    return await Misc.JpegBytesToBitmap(ThumbnailJpegBytes);    
}
+3
2

-, . () UI-. , , , UI-.

, , , . , Windows Store. , async ( ).

, , NotifyTaskCompletion AsyncEx, . : ( ), , , , PropertyChanged.

+4

... . .

, , , , . - . , .

CoreDispatcherPriority.Normal, , ; - - .

+1

All Articles