I have a WPF UI , On click of a button I am starting threads .
I want to read few txtbox values on the UI from the thread and want to update a textbox on the UI with the status of the thread
I am trying to pass the handle of the UI to the thread and get the window back from the handle ( as we do in MFC) and updat the UI.
I am able to get the handle as " IntPtr ParentHandle = new WindowInteropHelper(this).Handle;"
But dont know how to get the window from the handle .
OR
Is there anyother way i can update and retreive values on WPF UI from a thread .
Best Answer
No need to use any handles. Just pass object references. You can create the thread using the
ParameterizedThreadStart
class, which takes anobject
parameter. So you can define an object that contains members for all the values you want to pass to the thread, instead of having the thread retrieve them from the UI. Also, you can pass a reference to your window class, so when the thread is done (or to update the status), you can just use that reference (remember to use the Dispatcher to update the controls (in WinForms you'd have donethis.Invoke
).This could look like the following:
Please note that I'm writing this blindly without having testet this in WPF. This is, however, the way I do it all the time in WinForms.