Hello,
I want to use a simple routine: if user clicks a button, then the button disappears and progress bar shows up. After processing is done, the button becomes visible.
viewModel.StartRendering();
string script = viewModel.LilyScript;
ThreadStart x = delegate
{
Thread.Sleep(1000);
lily.ProcessScript(script);
Dispatcher.Invoke(new Action(delegate
{
viewModel.LilyRenderOutput = lily.ImageSource;
viewModel.EndRendering();
}), DispatcherPriority.Normal);
};
Thread thr = new Thread(x);
thr.Name = "Renderowanie";
thr.Start();
The viewModel has dep properties which are bound to controls' properties.
class MainWindowVM
{
...
public Visibility RenderingProgressBarVisibility...
public static readonly DependencyProperty ...
public Visibility PaintNotesButtonVisibility...
public static readonly DependencyProperty ...
public ImageSource LilyRenderOutput
internal void StartRendering()
{
RenderingProgressBarVisibility = Visibility.Visible;
PaintNotesButtonVisibility = Visibility.Hidden;
}
internal void EndRendering()
{
RenderingProgressBarVisibility = Visibility.Hidden;
PaintNotesButtonVisibility = Visibility.Visible;
}
}
I got an exception
The calling thread cannot access this object because a different thread owns it. on line
viewModel.LilyRenderOutput = lily.ImageSource;
In xaml:
<Image Source="{Binding LilyRenderOutput}" Stretch="None" />
Interesting fact: the code
Dispatcher.Invoke(new Action(delegate
{
this.Title="abc";
}), DispatcherPriority.Normal);
does work. Do I use a wrong dispatcher or what? I tried viewModel.Dispather with no success.
Any help appreciated.