Я пробовал использовать следующие рекомендации: http://msdn.microsoft.com/en-us/library/ms741870.aspx ("Обработка блокирующая операция с фоновым потоком ").
Это мой код:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
;
}
private void Process()
{
// some code creating BitmapSource thresholdedImage
ThreadStart start = () =>
{
Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(Update),
thresholdedImage);
};
Thread nt = new Thread(start);
nt.SetApartmentState(ApartmentState.STA);
nt.Start();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
button1.IsEnabled = false;
button1.Content = "Processing...";
Action action = new Action(Process);
action.BeginInvoke(null, null);
}
private void Update(ImageSource source)
{
this.image1.Source = source; // ! this line throw exception
this.button1.IsEnabled = true; // this line works
this.button1.Content = "Process"; // this line works
}
}
В отмеченной строке выдается исключение InvalidOperationException, «поскольку вызывающий поток не может получить доступ к этому объекту, потому что им владеет другой поток». Но приложение работает, если я удалю эту строку.
XAML:
Какая разница между свойством Image.Source и свойствами Button.IsEnabled и Button.Content? Что я могу сделать?