The IMvxMainThreadDispatcher now only allows you to run an action on the UI thread, regardless of the current thread.
However, sometimes you'd want to run different code depending on the thread you're on (e.g. when you're working with a synchronized, thread-safe observable collection).
So a property on the IMvxMainThreadDispatcher indicating if the current thread is the main thread would be very helpful.
Sounds like a good addition! Can you make a PR for this?
I will take a look at this issue.
So, I have been thinking a bit, and considered to expose a property called IsOnMainThread in the IMvxMainThreadAsyncDispatcher. Basically, this would do as follows:
public bool IsOnMainThread => Thread.CurrentThread.ManagedThreadId == 1;
I have although read that the main thread may not always be 1.
So, I considered persisting the mainThreadId when the instance is created, but found that the class was
abstract.
Any ideas?
That class doesn't need to be abstract i think. You could just remove that and add the code.
I am nearly done with my effort, @Cheesebaron and @martijn00, but wanted to know if you guys think it is the right direction to go:
https://github.com/david-laundav/MvvmCross/commits/feature/2791-Add-IsOnMainThread
As a note, it does build ;)
@david-laundav I don't think that we should be looking at the threadid. Currently each platform has their own implementation of checking to see if the current execution context is on the UI thread. Can you make the base property abstract and get each platform to implement it so that it's consistent?
Going forward if you can work off the v6.1 branch and submit a PR that'd help us with reviewing the changes. Thanks again for the submission
What is your proposal, @nickrandolph? I have spoken to @Cheesebaron about the approach, and both of us concurred that using the ThreadId would be the most efficient approach.
Please refer to the following:
https://forums.xamarin.com/discussion/comment/200008/#Comment_200008
Resolved by https://github.com/MvvmCross/MvvmCross/pull/2921
Most helpful comment
I will take a look at this issue.