Reputation: 23
I have a delegate function declared in the View (Window).
The delegate function is executed in a other class. But when i run the application and I the delegate function gets called i get the following error:
Exception types: System.invalidoperationException
Exception sources: WindowBase
Exception stack traces: System.Windows.threading.dispatcher.VerifyAcess() System.Windows.Threading.DispatcherObject.VerifyAccess() System.windows.Media.visual.verifyAPIReadWrite() ....
Upvotes: 1
Views: 1401
Reputation: 74802
This means that a function running in one thread is accessing a DispatcherObject "owned" by a different thread. DispatcherObjects (including DependencyObjects such as Visuals) can only be accessed from the thread where they were created. So I am guessing you are running your delegate on a different thread, e.g. via the thread pool or a BackgroundWorker.
The solution is to use Dispacther.Invoke or BeginInvoke when accessing properties or methods of the Visual. For example:
private void ThreadMethod()
{
// ...this is running on a thread other than the UI thread...
_myVisual.Dispatcher.Invoke(DoSomethingWithMyVisual);
}
private void DoSomethingWithMyVisual()
{
// because it has been called via Invoke, this will run on the UI thread
_myVisual.PartyOn();
}
Upvotes: 2