7

I was moving over a method to my winforms project from a wpf project.

Everything but this section was moved without issue:

private void ServerProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
  // You have to do this through the Dispatcher because this method is called by a different Thread
  Dispatcher.Invoke(new Action(() =>
  {
    richTextBox_Console.Text += e.Data + Environment.NewLine;
    richTextBox_Console.SelectionStart = richTextBox_Console.Text.Length;
    richTextBox_Console.ScrollToCaret();
    ParseServerInput(e.Data);
  }));
}

I have no idea how to convert over Dispatcher to winforms.

Can anyone help me out?

ErocM
  • 4,505
  • 24
  • 94
  • 161
  • *If your WPF code translates well enough to winforms, most probably you're doing it wrong. winforms' databindings capabilities are really limited compared to WPF's* – Federico Berasategui Aug 08 '13 at 19:52
  • 4
    @HighCore Sometimes, you have no choice about the technology you have to use – Shimrod Aug 08 '13 at 19:53
  • @Shimrod <-- has it right. – ErocM Aug 08 '13 at 19:55
  • @Shimrod I didn't say anything about the technology. I'm saying well-formed WPF code is fundamentally different from winforms code. – Federico Berasategui Aug 08 '13 at 19:55
  • @HighCore the C# code moved over just fine... not sure what the problem is? – ErocM Aug 08 '13 at 19:55
  • 2
    @HighCore: Ok, but what does that have to do with anything? He has to make the translation. What's your point? It's not like converting WPF to WinForms requires some brilliant technological insight. – Ed S. Aug 08 '13 at 19:56
  • I didn't mean to offence you, *"winforms' databindings capabilities are really limited compared to WPF's"* sounded a bit dogmatic. But I understand your point of view now. – Shimrod Aug 08 '13 at 19:57

1 Answers1

13

You should use Invoke to replace the Dispatcher.

private void ServerProcErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (richTextBox_Console.InvokeRequired)
    {
        richTextBox_Console.Invoke((MethodInvoker)delegate
        {
            ServerProcErrorDataReceived(sender, e);
        });
    }
    else
    {
        richTextBox_Console.Text += e.Data + Environment.NewLine;
        richTextBox_Console.SelectionStart = richTextBox_Console.Text.Length;
        richTextBox_Console.ScrollToCaret();
        ParseServerInput(e.Data);
    }
}
Shimrod
  • 3,115
  • 2
  • 36
  • 56