Is doing this less efficient than setting up a background worker thread? Because it is working well and seems cleaner. In the loop I am calling BeginInvoke 3x – to add rows to main window datagrid, and to update the progress bar and message.
public MyConstructor()
{
InitializeComponent();
ThreadStart start = delegate()
{
for (...)
{
...
MyWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(delegate()
{
// Do something on the MyWindow thread.
}
));
...
}
// Intensive loop now done and we close this processing window.
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(delegate()
{
this.Close();
}
));
}; // ThreadStart
new Thread(start).Start();
}
It’s OK-ish, but a few points:
You don’t really qualify ‘efficient’ very clearly, but BackgroundWorker is generally a better way of doing this sort of thing – if nothing else, it will use a pool thread, which is much cheaper than a manually created thread.