Spungo
Diamond Member
I'm trying to get into asynchronous tasks, and I'm having trouble understanding what thread has access to what data. In particular, I'm trying to pass data from the GUI (WPF) to new tasks that are performing CPU-intense work. If a checkbox is checked, do something a certain way. If a checkbox is not checked, do it a different way. In simple terms it's like this:
The following code does not work. It says PublishAllCheckBox.IsChecked is owned by a different thread.
The following code does work:
Using synchronous tasks for CPU-intense work freezes the GUI, so I assume the GUI and my code are the same thread. If that's the case, why are the tasks started by Task.Factory.StartNew() not able to get a variable from the GUI? Why do I need that new temporaryVariable?
The following code does not work. It says PublishAllCheckBox.IsChecked is owned by a different thread.
Code:
// all of this is in partial class MainWindow
private Task PublishEverythingAsync()
{
return Task.Factory.StartNew( () => PublishEverything(PublishAllCheckBox.IsChecked));
}
The following code does work:
Code:
// all of this is in partial class MainWindow
private Task PublishEverythingAsync()
{
bool? temporaryVariable = PublishAllCheckBox.IsChecked;
return Task.Factory.StartNew( () => PublishEverything(temporaryVariable));
}
Using synchronous tasks for CPU-intense work freezes the GUI, so I assume the GUI and my code are the same thread. If that's the case, why are the tasks started by Task.Factory.StartNew() not able to get a variable from the GUI? Why do I need that new temporaryVariable?