In some cases, we need to execute some time-consuming processes in the PS. Usually such process is triggered by the WPF dialog, e.g. by pressing the button. E.g. we have created a lot of components, guided by WPF dialogue and now by pressing button “Save” we have store something like 30 components to disk. Depending on the complexity of the components save operation can take some minutes. The user must be notified about the status of such operation.
In WPF in such case the time-consuming operations are executed in the background task and GUI will be updated from the dispatcher thread. This approach prevents “freezing” of the GUI and make possible progress indication.
During TEC# Ofer has mentioned, that Tecnomatix API is not a thread safe and all calls to API have been done from the “original” thread, which initiates the execution of the routine. So, what is the right pattern to implement the time-consuming operation in combination with PS and WPF.
it works in WinForms, but not in WPF. WPF updates the window just if Dispatcher thread is in wait state. If the dispatcher state is busy, the window is not updated.
Yes you are right. The WPF dialog should be created in a different thread. The tune thread (the main one), can communicates the WPF thread via the dispatcher.
my pragmatical solution is following:
I create WPF dialog in the Execute methos of the command class and start it calling Show method.
Than, e.g. by pressing a button, I start a new task for a time-consuming operation (Task.Factory.StartNew) and ensure that API called just from this task. To update UI I use Dispatcher.Invoke.
Is it ok from your point of view?