Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a store application where I am using a control from DLL (Based on Window Runtime component). My all View Models are in that DLL. In that DLL there is timer which updates the view (UI) and get exception "Called from different thread". I am not sure how to resolve this proble.
Posted
Comments
AghaKhan 26-Dec-13 0:28am    
C# Window store Application

The problem is totally unrelated to DLL, it is related to the use of the threading with UI. The timer even is not guaranteed to be invoked on the same thread, that's the problem.

You cannot call any method or property of the currently executing UI from any thread other then UI thread. For solving such problems, there is the method CoreDispatcher.RunAsync. It queues some callback with the parameters needed for the call to the UI thread; eventually, you call will be performed by the UI thread. This is the way you can notify the UI thread from other threads. The detail depends on exact language and API you use; please see:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.core.coredispatcher.aspx[^].

—SA
 
Share this answer
 
v2
Comments
AghaKhan 25-Dec-13 20:41pm    
Thank you kind email. In my solution I am using MVVM design pattern where all the ModelView are in DLL, but I am supplying data to DataContext (ModelViw) from main application. ModelView is creating a timer thread which updates the UI, which try to update the UI and get exception. Now how or where I have to call the CoreDispatcher.RunAsync function. Before your answer I looked http://mikaelkoskinen.net/fixing-coredispatcher-invoke-how-to-invoke-method-in-ui-thread-in-windows-8-release-preview/ and tried to supply ModelView to DLL object with RunAsync function which failed. DLL doesn't know anything about main application and how can (I can't) we update UI thread from DLL? It is a small application and if you want I can send you the application to look at it. Thanks for email.

Best regards
Agha Khan
Sergey Alexandrovich Kryukov 25-Dec-13 23:16pm    
What language are you using?
—SA
AghaKhan 26-Dec-13 3:38am    
public event PropertyChangedEventHandler PropertyChanged;
private async void NotifyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
var ignored = await CoreDispatcher.RunAsync(CoreDispatcherPriority.Normal,
() => { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); });
}
}
Error:
Error 1 An object reference is required for the non-static field, method, or property 'Windows.UI.Core.CoreDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority, Windows.UI.Core.DispatchedHandler)' C:\Apps\CSharp\Nuts\VisualControls\FlipViewModel.cs 36 37 VisualControls
Sergey Alexandrovich Kryukov 26-Dec-13 12:12pm    
What are you doing? RunAsync is not a static method!
You need to use the instance, which is obtained from CodeWindow.Dispatcher property.
—SA
AghaKhan 26-Dec-13 14:22pm    
:-) Thank you.

var coreDispatcher = new CoreDispatcher();
The type 'Windows.UI.Core.CoreDispatcher' has no constructors defined
What todo now?
Thanks for help.
private CoreDispatcher _dispatcher = null;
public event PropertyChangedEventHandler PropertyChanged;

private async Task UIThreadAction(Action act)
{
await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => act.Invoke());
}
private async void PropertyChangedAsync(string property)
{
if (PropertyChanged != null)
await UIThreadAction(() => PropertyChanged(this, new PropertyChangedEventArgs(property)));
}

public FlipViewModel()
{
_dispatcher = Window.Current.Dispatcher;
}

Thanks Sergey Alexandrovich Kryukov
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Dec-13 22:02pm    
Have some shame! This is not "solution" but pure cheating. You cannot do engineering by kicking the TV set until it start showing picture. You have no idea how to write code. Please stop spamming this site. If you need help, we will gladly help you, but I already told you what you should do.
—SA
AghaKhan 26-Dec-13 22:55pm    
Dear Brother:
Your comments are greatly appreciated. Someone should have to courage to say that. I have no problem with when you suggested “Have some shame”. Please guide me more. I love it. As you suggested "Implementation inheritance is not allowed in WinRT" is gibberish, but that is the error I received from my compiler and I am not making it up. I tried to implement solution but inheritance didn’t let me do so. I can send you the picture of my visual studio to show you that error if you wish. As you understand mistakes are the foundation of success and I do make mistakes. Please be my guide. Many times we write which is not following any design patterns but it works. Just Google “C# games by Agha Khan”. You will see about 4000 download.
Happy holidays

Best regards
Sergey Alexandrovich Kryukov
Thanks for help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900