I am currently making an application in which I have two threads running, and one often needs to set the Window.WindowStyle property, but I get an error when I try to do so (because my call isn’t thread-safe). I am already aware of how to use the InvokeRequired method of solving this but, when I type this.WindowStyle.InvokeRequired, Viusal Studio says InvokeRequired isn’t a valid property.
How can I fix this?
Please note that my application is a WPF app, not a Form. I would prefer answers to be in C# terminology, as I don’t know VB.NET very well.
Thanks for all your help!
EDIT: Ok, so this.Dispatcher.CheckAccess() seems to have fixed the initial problem, but I am now getting the same error when I attempt to actually invoke in the “if” statement. Any suggestions?
I think you will have more success if you use
SynchronizationContexthere instead of the olderISynchronizeInvokeinterface.Somewhere in the startup of your WPF application, you should create a synchronization context, something like this:
This code will create a synchronization context using the current dispatcher, so you would probably call this during initialization of your main window, after the dispatcher has started.
Next you use the
SendorPostmethods to marshal your code onto the GUI thread:There is a good article on synchronization contexts at code project: http://www.codeproject.com/KB/threads/SynchronizationContext.aspx, but it is specific to Windows Forms.