Will the variable uiContext go out scope after the event handler return? What does the compiler do behind the scene to make this code work.
private void findButton_Click(object sender, RoutedEventArgs e)
{
SynchronizationContext uiContext = SynchronizationContext.Current;
Task.Factory.StartNew(() =>
{
string pictures =
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
var folder = new DirectoryInfo(pictures);
FileInfo[] allFiles =
folder.GetFiles("*.jpg", SearchOption.AllDirectories);
FileInfo largest =
allFiles.OrderByDescending(f => f.Length).FirstOrDefault();
uiContext.Post(unusedArg =>
{
outputTextBox.Text = string.Format("Largest file ({0}MB) is {1}",
largest.Length / (1024 * 1024), largest.FullName);
},
null);
});
}