Skip to content

Instantly share code, notes, and snippets.

@SteffenBlake
Last active April 14, 2017 07:34
Show Gist options
  • Save SteffenBlake/d24a3a9a228aad465deb147e331d8d11 to your computer and use it in GitHub Desktop.
Save SteffenBlake/d24a3a9a228aad465deb147e331d8d11 to your computer and use it in GitHub Desktop.
private void Main_OnLoaded(object sender, RoutedEventArgs e)
{
var thread = new Thread(() => TextThread("Testing..."));
thread.Start();
}
private void TextThread(string text, Brush brush = null)
{
var brushFixed = brush ?? Brushes.Black;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ContextIdle, new Action(() => AddLine(text)));
for (var opacity = 0.01f; opacity <= 1f; opacity += 0.01f)
{
Thread.Sleep(1000);
var newBrush = brushFixed.Clone();
newBrush.Opacity = opacity;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ContextIdle, new Action(() => ChangeLine(newBrush)));
}
}
private void AddLine(string text)
{
if (!Block.Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new Action(() => AddLine(text)));
}
else
{
Block.Inlines.Add(new Run(text));
}
}
private void ChangeLine(Brush brush)
{
if (!Block.Dispatcher.CheckAccess())
{
Dispatcher.Invoke(new Action(() => ChangeLine(brush)));
}
else
{
Block.Inlines.Last().Foreground = brush;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment