-2

I'm posting this so that the code is available to whomever finds it helpful.

Slightly reduced code based on Noctis's suggestion.

StringBuilder DescriptionText = new StringBuilder();

async void RunDescription(StringBuilder description)
{
    DescriptionText = description;

    await Task.Delay(1000);  // Short delay before the text starts printing so the window has time to load

    new Thread(AddTextToTextBlock).Start();
}

void TextBlockDispatcher(string text)
{
    TextBlock1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => TextBlock1.Inlines.Add(text)));
}

void AddTextToTextBlock()
{
    foreach (char c in DescriptionText.ToString())
    {
        Thread.Sleep(30);
        TextBlockDispatcher(c.ToString());
    }
}
Cusha
  • 1
  • 3
  • 1
    Working code for which you want someone to review and make suggestions for, belongs on codereview.stackexchange.com. Even there, you need to provide a good [mcve] that illustrates clearly the aspects you want commentary on. Stack Overflow is for specific, practical _problems_ with code. – Peter Duniho Sep 23 '16 at 02:04
  • 1
    I'm voting to close this question as off-topic because it belongs on codereview.stackexchange.com – Jeff Sep 23 '16 at 02:49
  • @PeterDuniho Thanks, I'll keep that in mind in the future. – Cusha Sep 23 '16 at 09:20

1 Answers1

2

Sounds like the usual hoops you need to jump through. The only thing i might consider changing is instead of calling the dispatcher, and then checking if it has access, simply calling it directly on your object. It should look kinda like :

TextBlock1.Dispatcher.BeginInvoke((Action)(() => /* logic here */ )

Saves you an call, but same same really. You'll need to massage it in, since this is from the top of my head, but it should point you in the right direction.

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • If you saw my last comment, nevermind. Overlooked something silly. Your suggestion worked great, but I think I still might need at least one more method, which is fine. – Cusha Sep 23 '16 at 10:40