-
-
Notifications
You must be signed in to change notification settings - Fork 906
fix: make UiApplication instance thread-static instead of static
#1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ac2bf58 to
1c7495a
Compare
UiApplication instance thread-static instead of staticUiApplication instance thread-static instead of static
|
Sorry @pomianowski but I don't fully understand this PR. What is the use case of having an application-per-thread? In the example provided by @chucker, I think the problem is that he is trying to mess with the color of a button inside the wrong thread! Just use Dispatcher.BeginInvoke() to get the color changed properly. Could you elaborate on that? Maybe I am wroing, but currently I don't see a valid reason for this change. |
Generally speaking, WPF assumes that all UI takes place on a single thread, yes.
I don't think that's true. Can you provide an example of why that would be? It'll be instantiated again if accessed from a different thread, which should only be the case when it's needed in a different thread. My personal use case for using WPF controls (or brushes, or other resources) from multiple threads (but never one and the same object from different threads) is actually unit testing. Making NUnit or xUnit.net perform all tests on a single thread is tricky, so libraries like StaFact merely ensure one test is run in one and the same thread, but multiple tests can still run on different threads. See also this earlier comment of mine. |
Yes, sorry, I meant that. Since Moreover, it would be also possible to access the I don't know.. is this a "wanted" feature? Is this really necessary? Accessing MainWindow in the wrong thread offcourse would result in an exception, so maybe just making the App thread-local is safe, as you said. |
I don't see how they can address it. The way I look at it:
More generally speaking, I don't think the static members are a good design even if we only assume a single thread. WPF UI already does introduce DI in some places, and really, something like |
Ok I got your point. But still, I think there is mismatch between UiApplication being threadLocal vs its method/props like Shutdown(), MainWindow, Resources etc.. that acts on WPF singleton Application. That said, I understand there are some good benefits with PR 👍 |
Pull request type
Please check the type of change your PR introduces:
What is the current behavior?
Various controls don't safely handle a multi-threaded UI. See also #594.
An example stack trace would be:
What is the new behavior?
The root cause appears to be the static
UiApplicationinstance. By marking it with[ThreadStatic], we instead make the runtime create one instance per thread.Other information