I am working on a project of my company in which they used Dispatcher.Invoke()
i开发者_如何学Pythonn many places.If I am using BeginInvoke
instead of Invoke then the Synchronisation
between threads working fine but in case of Invoke the application is freezing and even not entering the execution to the delegate method also. Does anybody have any idea why it is happening like this?
Any answer will be appreciated.
Sample Code for Invoke
used in the Project:
Dispatcher.Invoke(DispatcherPriority.Send,
new DelegateMethod(MethodtoExecute));
private delegate void DelegateMethod();
void MethodtoExecute()
{
try
{
}
catch (Exception /*ex*/)
{
}
finally
{
}
}
Dispatcher.Invoke
executes synchronously on the same thread as your application, so whatever you invoke is able to block the main application thread. Dispatcher.BeginInvoke
executes asynchronously, so it doesn't tie up the main application thread while executing.
Since you are using DispatcherPriority.Send
, which is the highest dispatcher priority level, whatever you are invoking gets run before anything else, including rendering the screen or listening for events. I'd recommend switching that to DispatcherPriority.Background
, which runs at a lower priority than Render and Input. See this page for a list of the DispatcherPriority
levels and their execution order
I'd highly recommend you look at the answer posted here
精彩评论