The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) is raised at the only line in the following method in WorkScheduler.cs;
protected async void TakeFromPendingTasksAndRun()
{
await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false);
}
I have an ItemTemplate for displaying an image;
<ff:FFImage Name="Image"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
TransformPlaceholders="False"
LoadingPlaceholder="ImgLoading.png"
ErrorPlaceholder="ImgError.png"
CacheDuration="30"
RetryCount="3"
RetryDelay="250"
DownsampleToViewSize="True"
DownsampleMode="Default"
Source="{Binding ImageUri}" />
ImageUri is a property on an object that is in an ObservableCollection<T>
Can your help? I'm lost as to how to fix this.
Thanks!
Full exception detail is:
System.Exception was unhandled by user code
HResult=-2147417842
Message=The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
Source=Windows
StackTrace:
at Windows.UI.Xaml.DependencyObject.GetValue(DependencyProperty dp)
at FFImageLoading.FFImage.get_SuccessCommand()
at FFImageLoading.FFImage.OnSuccess(SuccessEventArgs e)
at FFImageLoading.FFImage.<LoadImage>b__12_2(ImageInformation imageInformation, LoadingResult loadingResult)
at FFImageLoading.Work.WorkScheduler.<>c__DisplayClass47_0.<CreateFrameworkTask>b__0(ImageInformation size, LoadingResult result)
at FFImageLoading.Work.ImageLoaderTask`2.<RunAsync>d__109.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at FFImageLoading.Work.WorkScheduler.<RunImageLoadingTaskAsync>d__50.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at FFImageLoading.Work.WorkScheduler.<RunImageLoadingTaskAsync>d__50.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at FFImageLoading.Work.WorkScheduler.<TakeFromPendingTasksAndRunAsync>d__49.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at FFImageLoading.Work.WorkScheduler.<TakeFromPendingTasksAndRun>d__46.MoveNext()
I've just proposed a fix for the same problem for Windows Phone 8.1 (waiting for merging).
In the meantime, you could try to apply the same solution to check if it works for you as well just change manually the method TakeFromPendingTasksAndRun in class FFImageLoading.Work.WorkScheduler
from this
protected async void TakeFromPendingTasksAndRun()
{
await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false); // FMT: we limit concurrent work using MaxParallelTasks
}
to this
protected void TakeFromPendingTasksAndRun()
{
Task.Factory.StartNew(async () =>
{
await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false); // FMT: we limit concurrent work using MaxParallelTasks
}, TaskCreationOptions.LongRunning).ConfigureAwait(false);
}
If you're using FFImageLoading in a list, you could find useful the improvement you can find here:
https://github.com/moonClimber/FFImageLoading/commit/e2ef7b5a3bc0f8324f99ba8da5359b303ca4457b
@wileecoyotesupergenius @moonClimber Could you test the newest prerelease if it solves that issue?
I will test it within the next week (Jan, 21)
Many thanks!
Thanks, I can also test next week (business trips getting in the way of coding!)
Best,
Simon
From: Omar Venturi notifications@github.com
Sent: January 12, 2017 11:06:14 AM
To: luberda-molinet/FFImageLoading
Cc: Simon Matthews; Mention
Subject: Re: [luberda-molinet/FFImageLoading] [UWP] Unhandled Exception: The application called an interface that was marshalled for a different thread. (#439)
I will test it within the next week (Jan, 21)
Many thanks!
-
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/luberda-molinet/FFImageLoading/issues/439#issuecomment-272203859, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJ4tcDKvQC1LgJv6Xd8b2cbJKbYzdhuKks5rRk92gaJpZM4Lc7Kn.
I've downloaded the 2.2.7 version from NuGet [1] but I think is the wrong version (the class FFImageSourceBindingCheckerCache is missing).
Where can I find the newest prerelease?
Thanks
[1] https://www.nuget.org/packages/Xamarin.FFImageLoading/
It's the correct version, it was moved here: https://github.com/luberda-molinet/FFImageLoading/blob/master/source/FFImageLoading.Windows/Cache/FFSourceBindingCache.cs :)
I've just tested it: the problem is still there, even if not as before.
Now, with TaskCreationOptions.PreferFairness, the works fine just the first time I open a view that uses FFImageLoading. When I open the same view the second time, I obtain the same exception I did before.
So, I've double checked that by using TaskCreationOptions.LongRunning everything always works fine.
If TaskCreationOptions.LongRunning causes inefficiencies in iOs and Android (I've not tested this), what about to use a compiler directive #if to use LongRunning with Windows and PreferFairness with others?
The real problem might be actually this:
If you change it to always force else part. Do you still experience it?
Same error. The problem doesn't seem to be there.
I can fix the problem by changing the TaskCreationOptions value only. Specifically, here are my tests (on Android and Windows Phone):
LongRunning: works fine
HideScheduler: works fine
PreferFairness: error happens
Actually, I've tried with HideScheduler option just as empirical approach. I don't actual know whether is a good option or not. Anyway, PrefairPairness seems to be definitely not the right choice.
Could you try the newest prerelease? It uses additional HideScheduler flag.
Any news?:)
I'm going to test it between today and tomorrow... I'll back to you really soon.
Thanks!
Hi Daniel and many thanks for your work!
Despite my tests, it seems that with version "2.2.8-pre-316" (the one I've used) the problem is still there. So, I've used again my local code with my changes and the app works.
I saw that your latest code uses this statement
TaskCreationOptions.PreferFairness | TaskCreationOptions.HideScheduler
whilst my code is just
TaskCreationOptions.HideScheduler
(and, as I've said previously, it works with TaskCreationOptions.LongRunning as well)
Hope to not get wrong and tell me if I can be useful some way. At this moment, I see the use of compiler directives as most effective solution, even if not so elegant.
Something like this pseudo-code:
`#if WINDOWS_PHONE
[...]TaskCreationOptions.LongRunning[...]
[your original code]
`
Hope you can find a more effective solution or just apply the above one (waiting for a better solution).
Thanks
Are you sure it works with TaskCreationOptions.HideScheduler? If it's the case I would prefer to use it instead TaskCreationOptions.LongRunning.
I'm sure. It works with HideScheduler and LongRunning (one or other separately, not with both options such as HideScheduler | LongRunning)
You could try 2.2.8-pre-321, I hope it'll solve the issue.
Also would it be possible for you to test this:
C#
Task.Factory.StartNew(async () =>
{
await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false); // FMT: we limit concurrent work using MaxParallelTasks
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach | TaskCreationOptions.HideScheduler, TaskScheduler.Default).ConfigureAwait(false);
?
With the second test the error is still there the second time I open the view whilst the first time work. This is the same behavior I have with PreferFairness.
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
now I'm going to test the prerelase
Sorry, there was a mistake on my side with previous comment. I've repeated the second test and actually it works, but with a strange behavior: the cache seems to not work.
With other configurations (HideScheduler and LongRunning) all the images were immediately visible, whilst with this configuration many images are downloaded again (available after a while).
This is the code I've used
protected void TakeFromPendingTasksAndRun()
{
Task.Factory.StartNew(async () =>
{
await TakeFromPendingTasksAndRunAsync().ConfigureAwait(false);
// FMT: we limit concurrent work using MaxParallelTasks
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach | askCreationOptions.HideScheduler, TaskScheduler.Default).ConfigureAwait(false);
}
Sorry for these confused comments. Tested again (because the behavior related to cache was too strange for me).
I can confirm that both your proposals now works (prerelease and TaskCreationOptions.DenyChildAttach | askCreationOptions.HideScheduler)
Thanks!
@moonClimber That's great. I would prefer to use the second approach (TaskCreationOptions.DenyChildAttach | TaskCreationOptions.HideScheduler, TaskScheduler.Default) as it's a safer option (task is executed using thread pool).
Thank you for your help. 馃憤
It's weird all that was needed for WP.
WP is really a strange beast !
Nice job @moonClimber and @daniel-luberda for figuring this out :+1:
Btw do you think it fixed #166 too?
@molinch Yes, that's a really strange issue as MainThreadDispatcher should take care of it. Yes it'll also fix #166
@moonClimber Can you confirm that 2.1.8 fixed the issue, so we could close those issues?
Dear Daniel, I've tested the version 2.2.8 (available today) and unfortunately it not work.
Now, I actually don't know why, it's really strange, but to try to solve this problem, I've created a simple project through whom isolate the problem.
It would be useful if other people could try to test and replicate the problem as well. The project is available here https://github.com/moonClimber/FFIList
This project contains the source code downloaded from your GitHub (I'm not expert in Git so since I didn't know how to link your code inside my project then I've used the "copy&paste" approach) and a reference to the binary from NuGet.
I've used mvvmcross, but this is pretty transparent for the purpose of the project (and, in any case, used in really simple way).
The problem is simply replicable just by running the "FFIList.WindowsPhone" project then do the following actions:
With the project FFIList.WindowsPhone there is no error
Let me know
That's a tricky issue. We have to understand better what's going on first.
I have the same issue in the Version 2.2.8. I downgraded Xamarin.FFImageLoading to 2.1.5 and it works now for me.
With the current version 2.2.9 the issue is gone if i deploy the app to the local machine. In the Emulator and Smartphone it not work, still the same exception.
So, i try also 2.18 and with this version, all is working fine. No
Unhandled Exception: The application called an interface that was marshalled for a different thread
Exception.
There must be something after this version that makes this issue.
Maybe it helps!
Hallo, Brian here, using in an observablecollection and getting: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)), here is the XAML:
Grid.Column="0"
VerticalAlignment="Top"
HorizontalAlignment="Stretch"
TransformPlaceholders="False"
LoadingPlaceholder="Assets/icon_loading_light.png"
ErrorPlaceholder="Assets/Wide310x150Logo.scale-100.png"
CacheDuration="4"
RetryCount="3"
RetryDelay="250"
Width="140"
Height="80"
DownsampleToViewSize="True"
DownsampleMode="None"
Stretch="UniformToFill"
Source="{Binding photoImage}"/>
Is it possible to have some simple reproduction so I could test & debug it?
@daniel-luberda Attached is a simple project that reproduces this issue. Basically its the PropertyChanged event that causes the cross thread error. If you don't see the error, resize the window to a point where it scrolls, and re-launch it.
In the recent 2.2.10 stable build there was some fix related to Windows MainThreadDispatcher. Eg: https://github.com/luberda-molinet/FFImageLoading/commit/49a4e302748b81ef41b2f0791a82369d95f64a56
Please reopen if you still can reproduce that bug. Thanks.
@daniel-luberda there is a test project attached to the previous comment and problem is still there in 2.2.10. Please reopen the issue
https://github.com/luberda-molinet/FFImageLoading/commit/4204bfe33f026d0c8ac3ab1dd368df74d66cc173
It will be a default settings on Windows now. Thanks you for your help! :)
I'm getting the same issue when trying to get a stream
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
at Windows.UI.Xaml.Media.Imaging.WriteableBitmap.get_PixelBuffer()
at FFImageLoading.Extensions.WriteableBitmapExtensions.<AsJpegStreamAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at FFImageLoading.TaskParameterPlatformExtensions.<AsJPGStreamAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at XXX.<StartUploadAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
<<ViewAppeared>b__5>d.MoveNext()
var imageService = ImageService.Instance;
imageService.Config.ExecuteCallbacksOnUIThread = true;
var task = imageService.LoadStream((cancelToken) => FileInfo.GetImageStreamAsync());
var taskWithTransforms = task
.WithCache(FFImageLoading.Cache.CacheType.Disk)
.Transform(GetTransformations());
return taskWithTransforms.AsPNGStreamAsync();