No need to use any handles. Just pass object references. You can create the thread using the ParameterizedThreadStart
class, which takes an object
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 done this.Invoke
).
This could look like the following:
public class WPFWindow ...
{
private class ThreadData
{
public int Value1;
public string Value2;
public WPFWindow Window;
}
....
private void StartThread()
{
ThreadData tdata = new ThreadData();
tdata.Value1 = 42;
tdata.Value2 = "Hello World!";
tdata.Window = this;
Thread t = new Thread(new ParameterizedThreadStart(ThreadMethod));
t.Start(tdata);
}
private void ThreadMethod(object data)
{
ThreadData tdata = (ThreadData)data;
// ... process values here
// Update controls
if(tdata.Window.textbox.Dispatcher.CheckAccess())
{
// The calling thread owns the dispatcher, and hence the UI element
tdata.Window.textbox.AppendText(...);
}
else
{
// Invokation required
tdata.Window.textbox.Dispatcher.Invoke(DispatcherPriority.Normal, delegate);
}
}
}
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.