Replies: 1 comment 2 replies
-
One thing to remember is that, as long as you have a reference to it, a DispatcherQueue is usable from another thread. This is documented in TryEnqueue. DispatcherQueue is also marked as agile, so it is usable across threads/apartments. In the autos at the bottom of the VS window, you can see that Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread returns null. However, if you try to obtain it in the Start handler, the results are different. As you can see from the autos at the bottom, GetForCurrentThread returns something, the {...} indicates that it is a valid object here. This means two things. First Application.Start creates the DispatcherQueue for the main thread. Secondly, if you want to grab a reference to the DispatcherQueue instance, the callback is the best time to do it. The above shows obtaining a reference to the DispatcherQueue and storing it in a global variable. It does require that you check to see if the reference is valid, but you can use this instance to queue up work on the main thread's dispatcher queue. If anything, the worst you would have to do is get the existing process ID from the activated event args and then use AllowSetForegroundWindow to allow your single instance to bring itself to the front. |
Beta Was this translation helpful? Give feedback.
-
I am using C++/WinRT to develop a single-instance application, the expected behavior is that each time a new instance is launched, it will redirect to the default instance and activate the window. However, the document suggests, "It’s best to check for the need to redirect activation as early as possible in your app’s execution. ". This is difficult to do, I can’t activate the target window and switch it to the foreground in the Activated event handler according to the implementation method in #1439, because the Activated callback and DispatcherQueue are in different threads.
I want to know if there is a better way to implement this behavior.
Beta Was this translation helpful? Give feedback.
All reactions